@ -13,8 +13,8 @@ It wraps [mongodb-core](https://github.com/christkv/mongodb-core) and is availab
mongojs is easy to use:
``` js
var mongojs = require('mongojs');
var db = mongojs(connectionString, [collections]);
var mongojs = require('mongojs')
var db = mongojs(connectionString, [collections])
```
The connection string should follow the format described in [the mongo connection string docs](http://docs.mongodb.org/manual/reference/connection-string/).
@ -22,20 +22,20 @@ Some examples of this could be:
``` js
// simple usage for a local db
var db = mongojs('mydb', ['mycollection']);
var db = mongojs('mydb', ['mycollection'])
// the db is on a remote server (the port default to mongo)
var db = mongojs('example.com/mydb', ['mycollection']);
var db = mongojs('example.com/mydb', ['mycollection'])
// we can also provide some credentials
var db = mongojs('username:password@example.com/mydb', ['mycollection']);
var db = mongojs('username:password@example.com/mydb', ['mycollection'])
// connect using SCRAM-SHA-1 mechanism
var db = mongojs('username:password@example.com/mydb', ['mycollection'], {authMechanism: 'ScramSHA1'});
var db = mongojs('username:password@example.com/mydb', ['mycollection'], {authMechanism: 'ScramSHA1'})
// connect now, and worry about collections later
var db = mongojs('mydb');
var mycollection = db.collection('mycollection');
var db = mongojs('mydb')
var mycollection = db.collection('mycollection')
```
__Attention MongoDB 3 users:__ In MongoDB 3 the default auth mechanism is ScramSHA1 not MongoCR (the default auth mechanism in mongojs). When connecting to an auth enabled MongoDB 3 instance providing the authMechanism option value 'ScramSHA1' is mandatory!
@ -45,64 +45,64 @@ The format for callbacks is always `callback(error, value)` where error is null
``` js
// find everything
db.mycollection.find(function(err, docs) {
db.mycollection.find(function(err, docs) {
// docs is an array of all the documents in mycollection
Notice that you should pipe the cursor through a stringifier (like [JSONStream](https://github.com/dominictarr/JSONStream))
@ -142,12 +142,12 @@ if you want to pipe it to a serial stream like a http response.
If you are using a capped collection you can create a [tailable cursor](http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/) to that collection by adding `tailable:true` to the find options
``` js
var cursor = db.mycollection.find({}, {}, {tailable:true, timeout:false});
var cursor = db.mycollection.find({}, {}, {tailable:true, timeout:false})
// since all cursors are streams we can just listen for data
cursor.on('data', function(doc) {
console.log('new document', doc);
});
cursor.on('data', function(doc) {
console.log('new document', doc)
})
```
Note that you need to explicitly set the selection parameter in the `find` call.
@ -157,17 +157,17 @@ Note that you need to explicitly set the selection parameter in the `find` call.
With mongojs you can run database commands just like with the mongo shell using `db.runCommand()`