Mongoose api:http://mongoosejs.com/docs/api.html
Mongoose Links
var mongoose = require (' Mongoose ');
var db = mongoose.createconnection (' Mongodb://127.0.0.1:27017/nodejs ');
Link error
Db.on (' Error ', function (error) {
Console.log (Error);
});
Schema structure
var mongooseschema = new Mongoose. Schema ({
Username: {type:string, default: ' Anonymous user '},
Title: {type:string},
Content: {type:string},
Time: {type:date, Default:Date.now},
Age: {Type:number}
});
Adding Mongoose instance methods
MongooseSchema.methods.findbyusername = function (username, callback) {
Return This.model (' Mongoose '). Find ({Username:username}, callback);
}
Adding Mongoose static methods, static methods can be used at the model level
MongooseSchema.statics.findbytitle = function (title, callback) {
Return This.model (' Mongoose '). Find ({Title:title}, callback);
}
Model
var Mongoosemodel = Db.model (' Mongoose ', Mongooseschema);
Adding records based on entity operations
var doc = {username: ' emtity_demo_username ', title: ' Emtity_demo_title ', content: ' Emtity_demo_content '};
var mongooseentity = new Mongoosemodel (DOC);
Mongooseentity.save (function (Error) {
if (Error) {
Console.log (Error);
} else {
Console.log (' Saved ok! ');
}
Close Database Link
Db.close ();
});
Adding records based on model operations
var doc = {username: ' model_demo_username ', title: ' Model_demo_title ', content: ' Model_demo_content '};
Mongoosemodel.create (Doc, function (Error) {
if (Error) {
Console.log (Error);
} else {
Console.log (' Save OK ');
}
Close Database Link
Db.close ();
});
Modify a record
Mongoosemodel.update (conditions, update, options, callback);
var conditions = {username: ' model_demo_username '};
var update = {$set: {age:27, title: ' Model_demo_title_update '}};
var options = {Upsert:true};
Mongoosemodel.update (conditions, update, options, function (Error) {
if (Error) {
Console.log (Error);
} else {
Console.log (' Update ok! ');
}
Close Database Link
Db.close ();
});
Inquire
Query based on an instance method
var mongooseentity = new Mongoosemodel ({});
Mongooseentity.findbyusername (' Model_demo_username ', function (error, result) {
if (Error) {
Console.log (Error);
} else {
Console.log (result);
}
Close Database Link
Db.close ();
});
A query based on static methods
Mongoosemodel.findbytitle (' Emtity_demo_title ', function (error, result) {
if (Error) {
Console.log (Error);
} else {
Console.log (result);
}
Close Database Link
Db.close ();
});
Mongoose Find
var criteria = {title: ' Emtity_demo_title '}; Query criteria
var fields = {title:1, content:1, time:1}; Fields to return
var options = {};
Mongoosemodel.find (Criteria, fields, options, function (error, result) {
if (Error) {
Console.log (Error);
} else {
Console.log (result);
}
Close Database Link
Db.close ();
});
Deleting records
var conditions = {username: ' emtity_demo_username '};
Mongoosemodel.remove (conditions, function (error) {
if (Error) {
Console.log (Error);
} else {
Console.log (' delete ok! ');
}
Close Database Link
Db.close ();
});
Original http://www.open-open.com/lib/view/open1375885691296.html
node. js Next Mongoose Simple Operation instance