Node through the mongoose to realize the deletion and modification of MongoDB

Source: Internet
Author: User

Node through the mongoose to realize the deletion and modification of MongoDB

New File Test.jsThe contents are as follows:
var mongoose = require (' Mongoose '), Schema = Mongoose. Schema;mongoose.connect (' mongodb://localhost/test '); var blogschema = new Schema ({   id        : {type:number, index: True}  , title       : {type:string}}), Mongoose.model ("blog", Blogschema), var blog = mongoose.model ("blog"); Get model instance var blog1 = new Blog (), blog1.id = 4;blog1.title= "ully"; Blog1.save (function (err) {  //store  if (err) {    Console.log (' Save Failed ');  }  Console.log (' save Success ');}); Blog.find ({id:4},function (Err,docs) {///Query ID 4 record     console.log (docs);     Console.log (' find success ');}); Blog.update ({id:4,title: "Upill"},function (err,docs) {//Update     Console.log (docs);     Console.log (' update Success ');}); Blog.remove ({id:4},function (Err,docs) {//delete records with ID 4     console.log (docs);     Console.log (' remove success ');});

Note: When performing a database view in Monogo, to query blogs (here blogs is because the model of the blog, I think this is the mongoose automatically generated table structure) table, that is

Use test; Db.blogs.find ();

Node through the mongoose to realize the deletion and modification of 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.