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

Async / Await Support #368

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,31 @@ Version > 1.0.x is a major rewrite of mongojs. So expect some things not to work

* __Removed__ `mongojs.connect` use `mongojs()` directly instead

## Async/Await Support

To have mongojs behave even more like the official MongoDB repl, rather than use callbacks, you can query or update the database using async/await. This works wherever callbacks can be used. Just omit the callback and use the await keyword. Don't forget to use the "async" keyword on your caller function.

```js

(async function(){ // you need an async caller.

// insert a record in mycollection
await db.mycollection.insert({hello: 'world'})

// update it
await db.mycollection.update({$set: {hello: 'again'}})

// find everything. when not providing a callback, collection.find() returns a cursor. This is similar to how the MongoDB Shell iterates 20 records at a time. To get all results immediately, call toArray()
var docs = await db.mycollection.find().toArray() // docs is an array of all the documents in mycollection

// find a document using a native ObjectId
var doc = db.mycollection.findOne({_id: mongojs.ObjectId('523209c4561c640000000001')}
// and so on, see earlier examples.

})()

```

# API

This API documentation is a work in progress.
Expand Down
Loading