Skip to content

Commit

Permalink
Merge pull request #8321 from dandv/patch-1
Browse files Browse the repository at this point in the history
Illustrate Promise API, move Import after Install
  • Loading branch information
vkarpov15 committed Nov 11, 2019
2 parents e9c69b3 + 689791b commit 5c04b5d
Showing 1 changed file with 20 additions and 15 deletions.
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

0 comments on commit 5c04b5d

Please sign in to comment.