Mongoose is the object Model tool for MongoDB. The main reason for the combination of Nodejs and MongoDB is that it has the same data storage format JSON, which has very good continuity in the application layer and does not require much code for data conversion.
Just as PHP and Java connect to MySQL, the Nodejs connection to MongoDB also requires a driver driver. MongoDB driver still have some, the most famous of which is native driver:https://github.com/mongodb/node-mongodb-native, The project is maintained by MongoDB's creation company 10gen, which provides some basic APIs for connection and data manipulation.
What we are talking about today is actually a package on top of the mongodb-native, which makes our operation on the data mongoose based on the model. Mongoose has some key concepts:
Schema (Schema): A database model skeleton stored as a file, not capable of database operation
Model (model): Modeled by schema release, database operations with abstract properties and behavior
Entity: An entity created by model whose actions affect the database
The relationship between them is that schema generation Model,model create Entity,model and entity can affect database operations, but model is more operational than entity.
Http://www.cnblogs.com/cubika/p/3501887.html
Define Schema
var userschema = new Mongoose. Schema ({
Name: {
First:string,
Last: {type:string, trim:true}
},
Age: {type:number, min:0}
});
Publish the schema as model
var puser = Mongoose.model (' powerusers ', userschema);
Creating an entity Using Model
var johndoe = new Puser ({
Name: {first: ' John ', Last: ' Doe '},
Age:25
});
Save entity to Database
Johndoe.save (function (err) {if (err) console.log (' Error on Save! ')});
Project:
Http://www.cnblogs.com/ycm119/p/3731945.html
Http://www.cnblogs.com/hubwiz/p/4091971.html
Http://www.cnblogs.com/moyiqing/p/mongoose.html
Http://www.cnblogs.com/edwardstudy/p/4092317.html
Mongoose is just a client of MongoDB, to be connected to MongoDB or must manually start MongoDB server side.
- To start the MongoDB client:
Way One:
var Dburi = ' mongodb://localhost:27018/mydatabase ';
Mongoose.connect (Dburi);
Way two:
var Dburi = ' mongodb://localhost:27018/mydatabase ';
var adminconnection = mongoose.createconnection (Dburi);
Get schema class
var mongoose = require (' Mongoose ');
var Schema = Mongoose. Schema;
Creating a Schema instance object
var nodeschema = new Schema ({
Name:string,
Age:number
});
Instantiate model
Mongoose.model (' Node ', nodeschema);
Create entity
var node = new node ({name: ' Edward ', Age: ' 23 '});
Node.save (function (err) {
if (err) {
Console.log (ERR);
}else{
Console.log (' The new node is saved ');
}
});
The Mongose of NODEJS study notes