The Mongose of NODEJS study notes

Source: Internet
Author: User
Tags mongodb client mongodb driver mongodb server

Mongoose is the object Model tool for MongoDB. The main reason for the combination of Nodejs and MongoDB is that it has the same data storage format JSON, which has very good continuity in the application layer and does not require much code for data conversion.

Just as PHP and Java connect to MySQL, the Nodejs connection to MongoDB also requires a driver driver. MongoDB driver still have some, the most famous of which is native driver:https://github.com/mongodb/node-mongodb-native, The project is maintained by MongoDB's creation company 10gen, which provides some basic APIs for connection and data manipulation.

What we are talking about today is actually a package on top of the mongodb-native, which makes our operation on the data mongoose based on the model. Mongoose has some key concepts:

Schema (Schema): A database model skeleton stored as a file, not capable of database operation

Model (model): Modeled by schema release, database operations with abstract properties and behavior

Entity: An entity created by model whose actions affect the database

The relationship between them is that schema generation Model,model create Entity,model and entity can affect database operations, but model is more operational than entity.

Http://www.cnblogs.com/cubika/p/3501887.html

Define Schema

var userschema = new Mongoose. Schema ({

Name: {

First:string,

Last: {type:string, trim:true}

},

Age: {type:number, min:0}

});

Publish the schema as model

var puser = Mongoose.model (' powerusers ', userschema);

Creating an entity Using Model

var johndoe = new Puser ({

Name: {first: ' John ', Last: ' Doe '},

Age:25

});

Save entity to Database

Johndoe.save (function (err) {if (err) console.log (' Error on Save! ')});

Project:

Http://www.cnblogs.com/ycm119/p/3731945.html

Http://www.cnblogs.com/hubwiz/p/4091971.html

Http://www.cnblogs.com/moyiqing/p/mongoose.html

Http://www.cnblogs.com/edwardstudy/p/4092317.html

Mongoose is just a client of MongoDB, to be connected to MongoDB or must manually start MongoDB server side.

    • To start the MongoDB client:

Way One:

var Dburi = ' mongodb://localhost:27018/mydatabase ';

Mongoose.connect (Dburi);

Way two:

var Dburi = ' mongodb://localhost:27018/mydatabase ';

var adminconnection = mongoose.createconnection (Dburi);

    • To create a data model:

Get schema class

var mongoose = require (' Mongoose ');

var Schema = Mongoose. Schema;

Creating a Schema instance object

var nodeschema = new Schema ({

Name:string,

Age:number

});

Instantiate model

Mongoose.model (' Node ', nodeschema);

Create entity

var node = new node ({name: ' Edward ', Age: ' 23 '});

Node.save (function (err) {

if (err) {

Console.log (ERR);

}else{

Console.log (' The new node is saved ');

}

});

The Mongose of NODEJS study notes

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.