Skip to content

Commit 5c04b5d

Browse files
authored
Merge pull request #8321 from dandv/patch-1
Illustrate Promise API, move Import after Install
2 parents e9c69b3 + 689791b commit 5c04b5d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mongoose
22

3-
Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment.
3+
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.
44

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

25-
## Importing
26-
27-
```javascript
28-
// Using Node.js `require()`
29-
const mongoose = require('mongoose');
30-
31-
// Using ES6 imports
32-
import mongoose from 'mongoose';
33-
```
34-
3525
## Plugins
3626

3727
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).
@@ -55,6 +45,16 @@ First install [node.js](http://nodejs.org/) and [mongodb](https://www.mongodb.or
5545
$ npm install mongoose
5646
```
5747

48+
## Importing
49+
50+
```javascript
51+
// Using Node.js `require()`
52+
const mongoose = require('mongoose');
53+
54+
// Using ES6 imports
55+
import mongoose from 'mongoose';
56+
```
57+
5858
## Overview
5959

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

6666
```js
67-
const mongoose = require('mongoose');
68-
69-
mongoose.connect('mongodb://localhost/my_database', {
67+
await mongoose.connect('mongodb://localhost/my_database', {
7068
useNewUrlParser: true,
7169
useUnifiedTopology: true
7270
});
@@ -172,7 +170,14 @@ MyModel.find({}, function (err, docs) {
172170
});
173171
```
174172

175-
You can also `findOne`, `findById`, `update`, etc. For more details check out [the docs](http://mongoosejs.com/docs/queries.html).
173+
You can also `findOne`, `findById`, `update`, etc.
174+
175+
```js
176+
const instance = await MyModel.findOne({ ... });
177+
console.log(instance.my.key); // 'hello'
178+
```
179+
180+
For more details check out [the docs](http://mongoosejs.com/docs/queries.html).
176181

177182
**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:
178183

0 commit comments

Comments
 (0)