Nodejs using Mongoose to manipulate MongoDB

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.