{ "name": "mongoose", "description": "Mongoose MongoDB ODM", "version": "3.5.4", "author": { "name": "Guillermo Rauch", "email": "guillermo@learnboost.com" }, "keywords": [ "mongodb", "mongoose", "orm", "data", "datastore", "nosql", "odm", "sql", "db", "database" ], "dependencies": { "hooks": "0.2.1", "mongodb": "1.2.8", "ms": "0.1.0", "sliced": "0.0.3", "muri": "0.1.0" }, "devDependencies": { "mocha": "1.7.4", "node-static": "0.5.9", "dox": "0.3.1", "jade": "0.26.3", "highlight.js": "7.0.1", "markdown": "0.3.1" }, "directories": { "lib": "./lib/mongoose" }, "scripts": { "test": "make test" }, "main": "./index.js", "engines": { "node": ">= 0.4.0" }, "bugs": { "url": "https://github.com/learnboost/mongoose/issues/new", "email": "https://groups.google.com/group/mongoose-orm" }, "repository": { "type": "git", "url": "git://github.com/LearnBoost/mongoose.git" }, "homepage": "http://mongoosejs.com", "readme": "## What's Mongoose?\n\nMongoose is a [MongoDB](http://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment.\n\nDefining a model is as easy as:\n\n var Comment = new Schema({\n title : String\n , body : String\n , date : Date\n });\n\n var BlogPost = new Schema({\n author : ObjectId\n , title : String\n , body : String\n , buf : Buffer\n , date : Date\n , comments : [Comment]\n , meta : {\n votes : Number\n , favs : Number\n }\n });\n\n var Post = mongoose.model('BlogPost', BlogPost);\n\n## Documentation\n\n[mongoosejs.com](http://mongoosejs.com/)\n\n## Try it live\n\n## Installation\n\nThe recommended way is through the excellent [NPM](http://www.npmjs.org/):\n\n $ npm install mongoose\n\nOtherwise, you can check it in your repository and then expose it:\n\n $ git clone git://github.com/LearnBoost/mongoose.git node_modules/mongoose/\n\nAnd install dependency modules written on `package.json`.\n\nThen you can `require` it:\n\n require('mongoose')\n\n## Connecting to MongoDB\n\nFirst, we need to define a connection. If your app uses only one database, you should use `mongose.connect`. If you need to create additional connections, use `mongoose.createConnection`.\n\nBoth `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`.\n\n var mongoose = require('mongoose');\n\n mongoose.connect('mongodb://localhost/my_database');\n\nOnce connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`.\n\n**Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.\n\n## Defining a Model\n\nModels are defined through the `Schema` interface. \n\n var Schema = mongoose.Schema\n , ObjectId = Schema.ObjectId;\n\n var BlogPost = new Schema({\n author : ObjectId\n , title : String\n , body : String\n , date : Date\n });\n\nAside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:\n\n* [Validators](http://mongoosejs.com/docs/validation.html) (async and sync)\n* [Defaults](http://mongoosejs.com/docs/api.html#schematype_SchemaType-default)\n* [Getters](http://mongoosejs.com/docs/api.html#schematype_SchemaType-get)\n* [Setters](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set)\n* [Indexes](http://mongoosejs.com/docs/guide.html#indexes)\n* [Middleware](http://mongoosejs.com/docs/middleware.html)\n* [Methods](http://mongoosejs.com/docs/guide.html#methods) definition\n* [Statics](http://mongoosejs.com/docs/guide.html#statics) definition\n* [Plugins](http://mongoosejs.com/docs/plugins.html)\n* [psuedo-JOINs](http://mongoosejs.com/docs/populate.html)\n\nThe following example shows some of these features:\n\n var Comment = new Schema({\n name : { type: String, default: 'hahaha' }\n , age : { type: Number, min: 18, index: true }\n , bio : { type: String, match: /[a-z]/ }\n , date : { type: Date, default: Date.now }\n , buff : Buffer\n });\n\n // a setter\n Comment.path('name').set(function (v) {\n return capitalize(v);\n });\n\n // middleware\n Comment.pre('save', function (next) {\n notify(this.get('email'));\n next();\n });\n\nTake a look at the example in `examples/schema.js` for an end-to-end example of a typical setup.\n\n## Accessing a Model\n\nOnce we define a model through `mongoose.model('ModelName', mySchema)`, we can access it through the same function\n\n var myModel = mongoose.model('ModelName');\n\nOr just do it all at once\n\n var MyModel = mongoose.model('ModelName', mySchema);\n\nWe can then instantiate it, and save it:\n\n var instance = new MyModel();\n instance.my.key = 'hello';\n instance.save(function (err) {\n //\n });\n\nOr we can find documents from the same collection\n\n MyModel.find({}, function (err, docs) {\n // docs.forEach\n });\n\nYou can also `findOne`, `findById`, `update`, etc. For more details check out [this link](http://mongoosejs.com/docs/queries.html).\n\n**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:\n\n var conn = mongoose.createConnection('your connection string');\n var MyModel = conn.model('ModelName', schema);\n var m = new MyModel;\n m.save() // works\n\n vs\n\n var conn = mongoose.createConnection('your connection string');\n var MyModel = mongoose.model('ModelName', schema);\n var m = new MyModel;\n m.save() // does not work b/c the default connection object was never connected\n\n## Embedded Documents\n\nIn the first example snippet, we defined a key in the Schema that looks like:\n\n comments: [Comments]\n\nWhere `Comments` is a `Schema` we created. This means that creating embedded documents is as simple as:\n\n // retrieve my model\n var BlogPost = mongoose.model('BlogPost');\n\n // create a blog post\n var post = new BlogPost();\n\n // create a comment\n post.comments.push({ title: 'My comment' });\n\n post.save(function (err) {\n if (!err) console.log('Success!');\n });\n\nThe same goes for removing them:\n\n BlogPost.findById(myId, function (err, post) {\n if (!err) {\n post.comments[0].remove();\n post.save(function (err) {\n // do something\n });\n }\n });\n\nEmbedded documents enjoy all the same features as your models. Defaults, validators, middleware. Whenever an error occurs, it's bubbled to the `save()` error callback, so error handling is a snap!\n\nMongoose interacts with your embedded documents in arrays _atomically_, out of the box.\n\n## Middleware\n\nMiddleware is one of the most exciting features about Mongoose. Middleware takes away all the pain of nested callbacks.\n\nMiddleware are defined at the Schema level and are applied for the methods `init` (when a document is initialized with data from MongoDB), `save` (when a document or embedded document is saved).\n\nThere's two types of middleware:\n\n- Serial\n Serial middleware are defined like:\n\n .pre(method, function (next, methodArg1, methodArg2, ...) {\n // ...\n })\n\n They're executed one after the other, when each middleware calls `next`.\n\n You can also intercept the `method`'s incoming arguments via your middleware -- notice `methodArg1`, `methodArg2`, etc in the `pre` definition above. See section \"Intercepting and mutating method arguments\" below.\n\n\n- Parallel\n Parallel middleware offer more fine-grained flow control, and are defined like:\n\n .pre(method, true, function (next, done, methodArg1, methodArg2) {\n // ...\n })\n\n Parallel middleware can `next()` immediately, but the final argument will be called when all the parallel middleware have called `done()`.\n\n### Error handling\n\nIf any middleware calls `next` or `done` with an `Error` instance, the flow is interrupted, and the error is passed to the function passed as an argument.\n\nFor example:\n\n schema.pre('save', function (next) {\n // something goes wrong\n next(new Error('something went wrong'));\n });\n\n // later...\n\n myModel.save(function (err) {\n // err can come from a middleware\n });\n\n### Intercepting and mutating method arguments\n\nYou can intercept method arguments via middleware.\n\nFor example, this would allow you to broadcast changes about your Documents every time someone `set`s a path in your Document to a new value:\n\n schema.pre('set', function (next, path, val, typel) {\n // `this` is the current Document\n this.emit('set', path, val);\n\n // Pass control to the next pre\n next();\n });\n\nMoreover, you can mutate the incoming `method` arguments so that subsequent middleware see different values for those arguments. To do so, just pass the new values to `next`:\n\n .pre(method, function firstPre (next, methodArg1, methodArg2) {\n // Mutate methodArg1\n next(\"altered-\" + methodArg1.toString(), methodArg2);\n })\n\n // pre declaration is chainable\n .pre(method, function secondPre (next, methodArg1, methodArg2) {\n console.log(methodArg1);\n // => 'altered-originalValOfMethodArg1' \n \n console.log(methodArg2);\n // => 'originalValOfMethodArg2' \n \n // Passing no arguments to `next` automatically passes along the current argument values\n // i.e., the following `next()` is equivalent to `next(methodArg1, methodArg2)`\n // and also equivalent to, with the example method arg \n // values, `next('altered-originalValOfMethodArg1', 'originalValOfMethodArg2')`\n next();\n })\n\n### Schema gotcha\n\n`type`, when used in a schema has special meaning within Mongoose. If your schema requires using `type` as a nested property you must use object notation:\n\n new Schema({\n broken: { type: Boolean }\n , asset : {\n name: String\n , type: String // uh oh, it broke. asset will be interpreted as String\n }\n });\n\n new Schema({\n works: { type: Boolean }\n , asset : {\n name: String\n , type: { type: String } // works. asset is an object with a type property\n }\n });\n\n## API docs\n\nYou can find the [Dox](http://github.com/visionmedia/dox) generated API docs [here](http://mongoosejs.com/docs/api.html).\n\n## Getting support\n\n- Google Groups [mailing list](http://groups.google.com/group/mongoose-orm)\n- (irc) #mongoosejs on freenode\n- reporting [issues](https://github.com/learnboost/mongoose/issues/)\n- [10gen](http://www.mongodb.org/display/DOCS/Technical+Support)\n\n## Driver access\n\nThe driver being used defaults to [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) and is directly accessible through `YourModel.collection`. **Note**: using the driver directly bypasses all Mongoose power-tools like validation, getters, setters, hooks, etc.\n\n## Mongoose Plugins\n\nTake a peek at the [plugins search site](http://plugins.mongoosejs.com/) to see related modules from the community.\n\n## Contributing to Mongoose\n\n### Cloning the repository\n\n git clone git://github.com/LearnBoost/mongoose.git\n\n### Guidelines\n\nSee [contributing](http://mongoosejs.com/docs/contributing.html).\n\n## Credits\n\n[contributors](https://github.com/learnboost/mongoose/graphs/contributors)\n\n## License\n\nCopyright (c) 2010-2012 LearnBoost <dev@learnboost.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", "readmeFilename": "README.md", "_id": "mongoose@3.5.4", "dist": { "shasum": "921250ce5c0c699a7b57aa6ed7a0c9fafb6042fc" }, "_from": "mongoose@>=2.7.0" }