Mongoose Study Notes

Source: Internet
Author: User

1 Installing the Moogoose module, attaching the--save parameter add the package information to the Package.json file

NPM Install--save Moogoose

2 Load the Moogoose module and connect to the database

var mongoose = require (' Mongoose '); // Mongoose.connect (' Mongodb://mongod address IP (ipdress)/database name to be connected (databasename) '); Mongoose.connect (' mongodb://localhost/news ');

3 Modeling (structure)

var newsschema = Mongoose. Schmea ({    title:string    type:numer    category:array    ...});

4 Convert schema compilation to modal (at this point the modal is a class)

Newsmodel = Mongoose.model (' News ', Newsschema)

5 We can instantiate this class and do some work

var New Newsmodel ({title: "This is a title", "type": 1, "category": [{category_id: "1", "Category_name": "Domestic"   // This is a title

We can also add methods to the schema and add methods that need to be compiled into modal

function () {console.log (this = Mongoose.model (' News ', Newsschema); // instantiate and Invoke var New Newsmodel ({title: "This is a title", "type": 1, "category": [{category_id: "1", "Category_name": "Domestic"   //  output: It's a method to show the Title:this is a title

6 in front of the words, we are talking to the schema to add methods, after compiling, to create a modal to instantiate, since it is a database, save, query and other operations.

var New Newsmodel ({title: "This is a title", "type": 1, "category": [{category_id: "1", "Category_name": "Domestic"}]}) ; News.save (function(error,result) {   if(error) {     console.log (error);       } Else {       Console.log (result);   }});

Mongoose Study Notes

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.