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