The Mongoose of MongoDB learning

Source: Internet
Author: User
Tags object model

MongoDB Introduction:

MongoDB is a JavaScript-based database, the storage format is JSON, and node is a JavaScript-based environment (library), so node and MongoDB collocation can reduce the time and space cost of data conversion.

Mongoose Introduction:

is a MongoDB object Model tool that transforms data from a database into JavaScript objects for you to use in your application, encapsulating some of the common methods of adding and removing MongoDB to a document,

Getting Nodejs to manipulate the MongoDB database becomes more flexible and simple.

Prepare:

Be sure to start MongoDB service first, enter Mongodb\bin\, execute mongod--dbpath d:\data

D:\data is where data is stored, typically located at the root of the MongoDB installation.

The first step is to install mongoose in the project and introduce

Installation

CNPM Install Mongoose--save

Introduced

Const Mongoose = require('Mongoose');

Second step, connect the database

Connect to a local database

Let db = Mongoose.createconnection ('mongodb://localhost/testmongoose');

Then:

Set Data type

New Mongoose. Schema ({    name: {        type:string,        default: ' Username '    }, age    : {        type:number    },    Gender: {        type:string,        default: ' Female '}    });

Select Collection

Let Monmodel = Db.model ('user', monschema);

Simulating a data set

Let content = {name: 'Nick', Age: Gender: ' male '};

Instantiating an object and inserting data

Let Moninsert = new Monmodel (content);

Save and close the connection

Moninsert.save (Err) = {    if(err) {        console.log (err);     Else {        console.log (' Insert data successfully ');    }     // Close the database     db.close ();});

Execute this node file, insert data successfully!

Ding Ding ~ ~ There is data in the database!

Mongo.js Complete code:

//Introducing ModulesConst MONGOOSE = require (' Mongoose ');//connecting to a databaseLet db = Mongoose.createconnection (' Mongodb://localhost/testmongoose ');//Set Data typeLet Monschema =NewMongoose. Schema ({name: {type:string,default: ' username '}, Age: {Type:number}, Gender: {type:string,default: ' Female '    }});//Select CollectionLet Monmodel = Db.model (' user ', Monschema);//Data SetLet content = {name: ' Nick ', age:23, Gender: ' Male '};//instantiating an object and inserting dataLet Moninsert =NewMonmodel (content); Moninsert.save (err)= {    if(Err) {Console.log (err); } Else{Console.log (' successfully inserting data '); }    //Close the databasedb.close ();});

Delete and change the complete code:

//Introducing ModulesConst MONGOOSE = require (' Mongoose ');//connecting to a databaseLet db = Mongoose.createconnection (' Mongodb://localhost/testmongoose ');//Set Data typeLet Monschema =NewMongoose. Schema ({name: {type:string,default: ' username '}, Age: {Type:number}, Gender: {type:string,default: ' Female '    }});//Select CollectionLet Monmodel = Db.model (' user ', Monschema);//Inserting DatafunctionInsertData () {//Data SetLet content = {name: ' Nick ', age:23, Gender: ' Male '}; //instantiating an object and inserting dataLet Moninsert =NewMonmodel (content); Moninsert.save (ERR)= {        if(Err) {Console.log (err); } Else{Console.log (' successfully inserting data '); }        //Close the databaseDb.close (); });}//Delete DatafunctionDeleteData () {//conditions to removeLet del = {name: ' Nick '}; Monmodel.remove (Del, (err, result)= {        if(Err) {Console.log (err); } Else{Console.log (' Delete: ' +result); }        //Close the databaseDb.close (); });}//Modifying DatafunctionUpdateData () {//Original data field valueLet OldValue = {name: ' Nick '}; //Single Condition UpdateLet newData1 = {$set: {name: ' Windy Summer '}}; //Multi-Conditional updateLet NewData2 = {$set: {name: ' Windy Summer ', Gender: ' Female '}}; Monmodel.update (OldValue, NewData2, (err, result)= {        if(Err) {Console.log (err); } Else{Console.log (' Update '); }        //Close the databaseDb.close (); });}//Querying DatafunctionFindData () {//the field to queryLet content = {name: ' Windy Summer '}; Let Field= {name:1, age:1, gender:1}; Monmodel.find (Content, field, (err, result)= {        if(Err) {Console.log (err); } Else{console.log (result); }    });}//InsertData ();//DeleteData ();//UpdateData ();FindData ();

The Mongoose of MongoDB learning

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.