Detailed description of node. js's addition, deletion, modification, and query operations based on the mongoose module, nodejsmongoose

Source: Internet
Author: User

Detailed description of node. js's addition, deletion, modification, and query operations based on the mongoose module, nodejsmongoose

MongoDB

MongoDB is a Javascript-based database with a JSON storage format, while Node is also a JavaScript-based environment (library ), therefore, the combination of node and mongoDB can reduce the time space overhead caused by data conversion.

Mongoose

It is an object model tool of MongoDB. It converts data in the database into JavaScript objects for your use in applications. It encapsulates some common methods for MongoDB to add, delete, modify, and query documents, it makes NodeJS more flexible and simple to operate Mongodb databases.

Installation module mongoose

npm install mongoose

[Note] the mongoose module depends on mongodb.

Common npm commands

Npm install <name>-g installs the package to the global environment, while npm install <name> -- save installs the package, it writes the information to the package. json to facilitate later maintenance view npm remove <name> remove npm update <name> update npm root-g to view the global package installation path npm-v to view the npm version

Enable mongodb Database

Go to the directory where mongod is located and run the command./mongod -- dbpath = the location where the data is stored.

Example 1:./mongod -- dbpath = ../data/dbname

Example 2:./mongod -- dbpath = ../data/dbname -- port: Custom port number. The default port number is 27017. (You can understand it. It is not recommended. It is difficult to modify the default port number for later maintenance)

Insert data

// Introduce the module var mongoose = require ('mongoose'); // connect to the database var db = mongoose. createConnection ('mongodb: // 127.0.0.1: 27017/test'); // set the Data Type var monSchema = new mongooose. schema ({name: {type: String, default: "username"}, age: {type: Number}, sex: {type: String }}); // select the 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 ('data inserted successfully');} db. close ();});

Delete data

// Introduce the module var mongoose = require ('mongoose'); // connect to the database var db = mongoose. createConnection ('mongodb: // 127.0.0.1: 27017/test'); // set the Data Type var monSchema = new mongooose. schema ({name: {type: String, default: "name"}, age: {type: Number}, sex: {type: String }}); // select the set var monModel = db. model ('user', monSchema); // condition var del = {name: "Nick"}; monModel. remove (del, function (err, result) {if (err) {console. log (err);} else {console. log ("update");} db. close ();});

Modify data

// Introduce the module var mongoose = require ('mongoose'); // connect to the database var db = mongoose. createConnection ('apsaradb for mongodb: // 127.0.0.1: 27017/test'); // cosole. log (db); // set the Data Type var monSchema = new mongooose. schema ({name: {type: String, default: "name"}, age: {type: Number}, sex: {type: String }}); // select the set var monModel = db. model ('user', monSchema); // original data field value var oldValue = {name: "Nick"}; // 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 ();});

Query data

// Introduce the module var mongoose = require ('mongoose'); // connect to the database var db = mongoose. createConnection ('apsaradb for mongodb: // 127.0.0.1: 27017/test'); // cosole. log (db); // set the Data Type var monSchema = new mongooose. schema ({name: {type: String, default: "name"}, age: {type: Number}, sex: {type: String }}); // select the 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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.