Mongodb
MongoDB is a JavaScript-based database, stored in JSON format, and node is a JavaScript based environment (library), so the pairing of node and MongoDB can reduce the time and space overhead associated with data transformations.
Mongoose
MongoDB is an object model tool that converts data from a database into a JavaScript object for use in your application, encapsulating common methods such as MongoDB for some additions and deletions to the document, making the Nodejs operation MongoDB Database more flexible and simpler.
Install module Mongoose
[Note] Mongoose module relies on MongoDB
NPM Common Commands
NPM Install <name> g installs the package into the global environment with
NPM install <name>–-save installation, writes the information to the Package.json, facilitates later maintenance view
NPM Remove <name> Remove
npm update <name> update
NPM root-g View the Global package installation path
npm-v View NPM version
Open MongoDB Database
Enter the directory where the Mongod resides./mongod--dbpath= where to store data
Example 1:./mongod--dbpath=. /data/dbname
Example 2:./mongod--dbpath=. /data/dbname--port Custom port number, default 27017 (understanding can, not recommended, modify default port number later maintenance trouble)
Inserting data
Introducing Module
var mongoose = require (' Mongoose ');
Connection Database
var db = mongoose.createconnection (' mongodb://127.0.0.1:27017/test ');
Set data type
var Monschema = new Mongooose. Schema ({
name:{type:string,default: "username"},
Age:{type:number},
sex:{type:string}
);
Select Set
var Monmodel = Db.model (' user ', Monschema);
DataSet
var content = {Name: "Nick", Age:23,sex: ' Male '};
Instantiate the object and insert the data
var moninsert = new Monmodel (content);
Moninsert.save (function (err) {
if (err) {
console.log (err);
} else{
console.log (' Insert data successfully ');
}
Db.close ();
});
Delete data
Introducing Module
var mongoose = require (' Mongoose ');
Connection Database
var db = mongoose.createconnection (' mongodb://127.0.0.1:27017/test ');
Set data type
var Monschema = new Mongooose. Schema ({
name:{type:string,default: "Name"},
Age:{type:number},
sex:{type:string}
});
Select Set
var Monmodel = Db.model (' user ', Monschema);
The condition to be deleted is
var del = {name: "Nick"};
Monmodel.remove (Del,function (err,result) {
if (err) {
console.log (err);
} else{
console.log ("Update");
Db.close ();
});
modifying data
Introducing Module
var mongoose = require (' Mongoose ');
Connection Database
var db = mongoose.createconnection (' mongodb://127.0.0.1:27017/test ');
Cosole.log (db);
Set data type
var Monschema = new Mongooose. Schema ({
name:{type:string,default: "Name"},
Age:{type:number},
sex:{type:string}
});
Select Set
var Monmodel = Db.model (' user ', Monschema);
Original data field value
var oldValue = {name: "Nick"};
Single condition update
var newData1 = {$set: {name: Content}};
Multi-condition Update
var newData2 = {$set: {name: Content, Age:2}};
Monmodel.update (Oldvalue,newdata,function (err,result) {
if (err) {
console.log (err);
} else{
console.log ("Update");
Db.close ();
});
Querying data
Introducing Module
var mongoose = require (' Mongoose ');
Connection Database
var db = mongoose.createconnection (' mongodb://127.0.0.1:27017/test ');
Cosole.log (db);
Set data type
var Monschema = new Mongooose. Schema ({
name:{type:string,default: "Name"},
Age:{type:number},
sex:{type:string}
});
Select Set
var Monmodel = Db.model (' user ', Monschema);
var content = {Name: "Name 2"};
var field = {Name:1,age:1,sex:1};
Monmodel.find (Content,field,function (err,result) {
if (err) {
console.log (err);
} else{
Console.log (result);
}
Db.close ();
});
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.