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