node. js Next Mongoose Simple Operation instance

Source: Internet
Author: User

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

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.