demos
Version:
72 lines (71 loc) • 5.71 kB
JSON
{
"name": "mongojs",
"description": "Easy to use module that implements the mongo api",
"keywords": [
"mongo",
"db",
"mongodb"
],
"version": "0.7.17",
"repository": {
"type": "git",
"url": "git://github.com/gett/mongojs.git"
},
"contributors": [
{
"name": "Mathias Buus Madsen",
"email": "mathiasbuus@gmail.com"
},
{
"name": "Ian Jorgensen"
},
{
"name": "Eduardo Sorribas"
},
{
"name": "Taeho Kim"
},
{
"name": "Forbes Lindesay"
},
{
"name": "Robert S."
},
{
"name": "Srirangan"
},
{
"name": "Erkan Yilmaz"
},
{
"name": "Jake Maskiewicz"
},
{
"name": "Bjarke Walling"
},
{
"name": "Tobias Baunbæk"
},
{
"name": "Benedikt Arnold"
},
{
"name": "Kevin McTigue"
}
],
"dependencies": {
"mongodb": ">=1.2.8",
"thunky": "~0.1.0",
"readable-stream": "~1.0.2"
},
"scripts": {
"test": "node ./tests"
},
"readme": "# mongojs\nA [node.js](http://nodejs.org) module for mongodb, that emulates [the official mongodb API](http://www.mongodb.org/display/DOCS/Home) as much as possible. It wraps [mongodb-native](https://github.com/christkv/node-mongodb-native/).\nIt is available through npm:\n\n\tnpm install mongojs\n\n## Usage\n\nmongojs is easy to use:\n\n``` js\nvar mongojs = require('mongojs');\nvar db = mongojs(connectionString, [collections]);\n```\n\nThe connection string should follow the format desribed in [the mongo connection string docs](http://docs.mongodb.org/manual/reference/connection-string/).\nSome examples of this could be:\n\n``` js\n// simple usage for a local db\nvar db = mongojs('mydb', ['mycollection']);\n\n// the db is on a remote server (the port default to mongo)\nvar db = mongojs('example.com/mydb', ['mycollection']);\n\n// we can also provide some credentials\nvar db = mongojs('username:password@example.com/mydb', ['mycollection']);\n\n// connect now, and worry about collections later\nvar db = mongojs('mydb');\nvar mycollection = db.collection('mycollection');\n```\n\nAfter we connected we can query or update the database just how we would using the mongo API with the exception that we use a callback\nThe format for callbacks is always `callback(error, value)` where error is null if no exception has occured.\n\n``` js\n// find everything\ndb.mycollection.find(function(err, docs) {\n\t// docs is an array of all the documents in mycollection\n});\n\n// find everything, but sort by name\ndb.mycollection.find().sort({name:1}, function(err, docs) {\n\t// docs is now a sorted array\n});\n\n// iterate over all whose level is greater than 90.\ndb.mycollection.find({level:{$gt:90}}).forEach(function(err, doc) {\n\tif (!doc) {\n\t\t// we visited all docs in the collection\n\t\treturn;\n\t}\n\t// doc is a document in the collection\n});\n\n// find all named 'mathias' and increment their level\ndb.mycollection.update({name:'mathias'}, {$inc:{level:1}}, {multi:true}, function(err) {\n\t// the update is complete\n});\n\n// find one named 'mathias', tag him as a contributor and return the modified doc\n\ndb.mycollection.findAndModify({\n\tquery: { name: 'mathias' },\n\tupdate: { $set: { tag:'maintainer' } },\n\tnew: true\n}, function(err, doc) {\n\t// doc.tag === 'maintainer'\n});\n\n\n// use the save function to just save a document (the callback is optional for all writes)\ndb.mycollection.save({created:'just now'});\n```\n\nIf you provide a callback to `find` or any cursor config operation mongojs will call `toArray` for you\n\n``` js\ndb.mycollection.find({}, function(err, docs) { ... });\n\ndb.mycollection.find({}).limit(2).skip(1, function(err, docs) { ... });\n```\nis the same as\n\n``` js\ndb.mycollection.find({}).toArray(function(err, docs) { ... });\n\ndb.mycollection.find({}).limit(2).skip(1).toArray(function(err, docs) { ... });\n```\n\n\nFor more detailed information about the different usages of update and quering see [the mongo docs](http://www.mongodb.org/display/DOCS/Manual)\n\n## Streaming cursors\n\nAs of `0.7.0` all cursors are a [readable stream](http://nodejs.org/api/stream.html#stream_readable_stream) of objects.\n\n``` js\nvar JSONStream = require('JSONStream');\n\n// pipe all documents in mycollection to stdout\ndb.mycollection.find({}).pipe(JSONStream.stringify()).pipe(process.stdout);\n```\n\nNotice that you should pipe the cursor through a stringifier (like [JSONStream](https://github.com/dominictarr/JSONStream))\nif you want to pipe it to a serial stream like a http response.\n\n## Tailable cursors\n\nIf 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\n\n``` js\nvar cursor = db.mycollection.find({}, {}, {tailable:true, timeout:false});\n\n// since all cursors are streams we can just listen for data\ncursor.on('data', function(doc) {\n\tconsole.log('new document', doc);\n});\n```\n\nNote that you need to explicitly set the selection parameter in the `find` call.\n\n## Replication Sets\n\nMongojs can also connect to a mongo replication set by providing a connection string with multiple hosts\n\n``` js\nvar db = mongojs('rs-1.com,rs-2.com,rs-3.com/mydb?slaveOk=true', ['mycollection']);\n```\n\nFor more detailed information about replica sets see [the mongo replication docs](http://www.mongodb.org/display/DOCS/Replica+Sets)\n\n## License\n\nMIT\n",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/gett/mongojs/issues"
},
"_id": "mongojs@0.7.17",
"_from": "mongojs@"
}