Detailed Nodejs based on the Mongoose module to check the deletion of the Operation _node.js

Source: Internet
Author: User
Tags json mongodb port number

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

NPM Install 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.

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.