Mongoose官方API,我作完以後整理出來的心得。node
First be sure you have MongoDB and Node.js installed.mongodb
Next install Mongoose from the command line using npm:npm
$ npm install mongoose
項目中添加 mongoose模塊segmentfault
Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we need to do is include mongoose in our project and open a connection to the test database on our locally running instance of MongoDB.mongoose
// getting-started.js
var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test');
這裏面運行官方的代碼會有警告,查看緣由以後,我進行了以下修改ide
var mongoose = require('mongoose');
mongoose.Promise = global.Promise; mongoose.connect('mongodb://localhost/test',{useMongoClient:true});
We have a pending connection to the test database running on localhost. We now need to get notified if we connect successfully or if a connection error occurs:ui
var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { // we're connected!
});
這裏要注意,必須先開通mongodb的服務,node才能經過mongoose聯通dbthis
With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our kittens.spa
var kittySchema = mongoose.Schema({ name: String });
So far so good. We've got a schema with one property, name, which will be a String. The next step is compiling our schema into a Model..net
var Kitten = mongoose.model('Kitten', kittySchema);
A model is a class with which we construct documents. In this case, each document will be a kitten with properties and behaviors as declared in our schema. Let's create a kitten document representing the little guy we just met on the sidewalk outside:
var silence = new Kitten({ name: 'Silence' }); console.log(silence.name); // 'Silence'
Kittens can meow, so let's take a look at how to add "speak" functionality to our documents:
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () { var greeting = this.name ? "Meow name is " + this.name : "I don't have a name"; console.log(greeting); } var Kitten = mongoose.model('Kitten', kittySchema);
Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance:
var fluffy = new Kitten({ name: 'fluffy' }); fluffy.speak(); // "Meow name is fluffy"
We have talking kittens! But we still haven't saved anything to MongoDB. Each document can be saved to the database by calling its save method. The first argument to the callback will be an error if any occured.
fluffy.save(function (err, fluffy) { if (err) return console.error(err); fluffy.speak(); });
已經存進去了!
Say time goes by and we want to display all the kittens we've seen. We can access all of the kitten documents through our Kitten model.
Kitten.find(function (err, kittens) { if (err) return console.error(err); console.log(kittens); })
We just logged all of the kittens in our db to the console. If we want to filter our kittens by name, Mongoose supports MongoDBs rich querying syntax.
Kitten.find({ name: /^fluff/ }, callback);
這裏面用的是正則,也可使用指定字段!
This performs a search for all documents with a name property that begins with "Fluff" and returns the result as an array of kittens to the callback.
Congratulations
That's the end of our quick start. We created a schema, added a custom document method, saved and queried kittens in MongoDB using Mongoose. Head over to the guide, or API docs for more.
第一個
https://segmentfault.com/q/1010000010061553/a-1020000010070929
mongoose.connect('mongodb://localhost/test',{useMongoClient:true});
第二個
http://blog.csdn.net/fd214333890/article/details/53486862
mongoose.Promise = global.Promise;