Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illustrate Promise API, move Import after Install #8321

Merged
merged 1 commit into from Nov 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 20 additions & 15 deletions README.md
@@ -1,6 +1,6 @@
# Mongoose

Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment.
Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment. It has both a callbacks and Promise-style API.

[![Slack Status](http://slack.mongoosejs.io/badge.svg)](http://slack.mongoosejs.io)
[![Build Status](https://api.travis-ci.org/Automattic/mongoose.svg?branch=master)](https://travis-ci.org/Automattic/mongoose)
Expand All @@ -22,16 +22,6 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed
- [Help Forum](http://groups.google.com/group/mongoose-orm)
- [MongoDB Support](https://docs.mongodb.org/manual/support/)

## Importing

```javascript
// Using Node.js `require()`
const mongoose = require('mongoose');

// Using ES6 imports
import mongoose from 'mongoose';
```

## Plugins

Check out the [plugins search site](http://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](http://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins).
Expand All @@ -55,6 +45,16 @@ First install [node.js](http://nodejs.org/) and [mongodb](https://www.mongodb.or
$ npm install mongoose
```

## Importing

```javascript
// Using Node.js `require()`
const mongoose = require('mongoose');

// Using ES6 imports
import mongoose from 'mongoose';
```

## Overview

### Connecting to MongoDB
Expand All @@ -64,9 +64,7 @@ First, we need to define a connection. If your app uses only one database, you s
Both `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`.

```js
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/my_database', {
await mongoose.connect('mongodb://localhost/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true
});
Expand Down Expand Up @@ -172,7 +170,14 @@ MyModel.find({}, function (err, docs) {
});
```

You can also `findOne`, `findById`, `update`, etc. For more details check out [the docs](http://mongoosejs.com/docs/queries.html).
You can also `findOne`, `findById`, `update`, etc.

```js
const instance = await MyModel.findOne({ ... });
console.log(instance.my.key); // 'hello'
```

For more details check out [the docs](http://mongoosejs.com/docs/queries.html).

**Important!** If you opened a separate connection using `mongoose.createConnection()` but attempt to access the model through `mongoose.model('ModelName')` it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:

Expand Down