Nodejs operation MongoDB can use Mongoose:
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.
Install Mongoose:
NPM Install Mongoose
Get MongoDB Connection
var conn = mongoose.connect (' mongodb://localhost/mytest ');
var Schema = Mongoose. Schema
, ObjectId = Schema.objectid;
var person = new Schema ({
Title: {type:string}
, age: {type:number, Min:5, max:20}
, Meta: {
Likes: [String]
, Birth: {type:date}
}
});
var p = mongoose.model (' ModelName22 ', person);
var Blog = Mongoose.model ("ModelName22");
Save a new record
var blog1 = new Blog ();
Blog1.id22 = 4;
Blog1.title= "ully";
Blog1.save (function (err) {
if (err) {
Console.log (' save failed ');
}
Console.log (' save Success ');
});
Find Records
Blog.find ({_id: ' 4f8678891256c4b819000002 '},function (err,docs) {
Console.log (Docs);
});
Modify a record
var conditions = {name: ' Borne '}
, update = {$set: {title: ' Xxxxb '}}
, options = {};
Blog.update ({_id: ' 4f866f35311977a81b000001 '},update,options,function (err,docs) {
Console.log (docs+ "," +err);
});
Deleting records
Blog.remove ({_id: ' 4f8678891256c4b819000002 '},function (err,docs) {
Console.log (Docs);
});
Nodejs using Mongoose to manipulate MongoDB