Home > Article > Web Front-end > Detailed explanation of the environment configuration of Node.js using MongoDB under Wondows_node.js
In order to save the user data and business data of the website, a database is usually required. MongoDB and Node.js are particularly well matched because MongoDB is a non-relational database based on documents. Documents are stored in BSON (a lightweight binary format of JSON). The commands for managing the database such as addition, deletion, modification and query are very similar to JavaScript syntax. If you access MongoDB data in Node.js, you will feel like we are a family, which is very cordial.
I am also going to use MongoDB as my database.
MongoDB uses collections and documents to describe and store data. Collections are equivalent to tables, and documents are equivalent to rows. However, in relational databases such as MySQL, the table structure is fixed. For example, a row is composed of It consists of several columns, and the rows are the same. However, MongoDB is different. Multiple documents in a collection can have different structures, which is more flexible.
Install Mongo
The detailed guide is here (MongoDB’s official documentation): https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/.
Go to https://www.mongodb.org/downloads to download the installation package. The Windows system is an msi file. I chose the "Windows 64-bit 2008 R2+" version.
The installation is very simple. You can default or choose the installation location. I installed it in the MongoDB directory of G drive. After installation, the directory structure is as follows: G:MongoDBServer3.0.
Mongod, mongo and other tools are all in the bin directory under the 3.0 directory.
Start
To use MongoDB, you need to specify a folder to store data. I created a folder named db under G:MongoDB.
Open cmd, enter the G:MongoDBServer3.0bin directory, execute "mongod –dbpath=G:MongoDBdb", MongoDB will be started, and you will see the following picture:
After MongoDB is started, it will listen on a port and wait for the client to connect. As can be seen from the above figure, the default listening port is 27017. You can change this port with the “–port” option. For example, the “mongod –port 28018 –dbpath=G:MongoDBdb” command will start MongoDB and listen to port 28018.
After starting MongoDB, we can use mongo (interactive shell) to manage the database. Execute mongo directly in the bin directory, and you can see the following picture:
Mongo Shell connects to the test database by default, and also tells us that we can enter help to view help. You can type help and press Enter to see what commands are available.
Note that mongod starts without authentication by default. Once the client is connected, it can operate at will, including database creation, addition, deletion, modification, etc. If you want to limit user permissions, you can configure it yourself. I will go straight down here.
Install mongoose driver
Install GIT tools:
Since the github website does not support direct downloading of source code packages that package all submodules, you need to check out all source codes through the git tool. From http://code.google.com/p/msysgit/downloads/list, you can download msysgit, a git client tool under the Windows platform (the latest version file is Git-1.7.7.1-preview20111027.exe). After downloading, double-click to install.
Download NPM source code:
Open the command line tool (CMD) and execute the following command to check out all NPM source code and dependent codes through msysgit and install npm.
git clone --recursive git://github.com/isaacs/npm.git cd npm node cli.js install npm -gf
Before executing this code, please ensure that node.exe is installed through node.msi or is in the PATH environment variable. This command will also add npm to the PATH environment variable, and you can then execute the npm command anywhere. If you encounter permission errors during installation, make sure the cmd command line tool is run as an administrator. After successful installation, execute the following command:
npm install underscore
Return:
underscore@1.2.2 ./node_modules/underscore
In this way, NPM under the Windows platform is installed, and then we can install mongoose
npm install mongoose
Example
Some basic operations and instructions are written in code comments:
// mongoose 链接 var mongoose = require('mongoose'); var db = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS'); // 链接错误 db.on('error', function(error) { console.log(error); }); // Schema 结构 var mongooseSchema = new mongoose.Schema({ username : {type : String, default : '匿名用户'}, title : {type : String}, content : {type : String}, time : {type : Date, default: Date.now}, age : {type : Number} }); // 添加 mongoose 实例方法 mongooseSchema.methods.findbyusername = function(username, callback) { return this.model('mongoose').find({username: username}, callback); } // 添加 mongoose 静态方法,静态方法在Model层就能使用 mongooseSchema.statics.findbytitle = function(title, callback) { return this.model('mongoose').find({title: title}, callback); } // model var mongooseModel = db.model('mongoose', mongooseSchema); // 增加记录 基于 entity 操作 var doc = {username : 'emtity_demo_username', title : 'emtity_demo_title', content : 'emtity_demo_content'}; var mongooseEntity = new mongooseModel(doc); mongooseEntity.save(function(error) { if(error) { console.log(error); } else { console.log('saved OK!'); } // 关闭数据库链接 db.close(); }); // 增加记录 基于model操作 var doc = {username : 'model_demo_username', title : 'model_demo_title', content : 'model_demo_content'}; mongooseModel.create(doc, function(error){ if(error) { console.log(error); } else { console.log('save ok'); } // 关闭数据库链接 db.close(); }); // 修改记录 mongooseModel.update(conditions, update, options, callback); var conditions = {username : 'model_demo_username'}; var update = {$set : {age : 27, title : 'model_demo_title_update'}}; var options = {upsert : true}; mongooseModel.update(conditions, update, options, function(error){ if(error) { console.log(error); } else { console.log('update ok!'); } //关闭数据库链接 db.close(); }); // 查询 // 基于实例方法的查询 var mongooseEntity = new mongooseModel({}); mongooseEntity.findbyusername('model_demo_username', function(error, result){ if(error) { console.log(error); } else { console.log(result); } //关闭数据库链接 db.close(); }); // 基于静态方法的查询 mongooseModel.findbytitle('emtity_demo_title', function(error, result){ if(error) { console.log(error); } else { console.log(result); } //关闭数据库链接 db.close(); }); // mongoose find var criteria = {title : 'emtity_demo_title'}; // 查询条件 var fields = {title : 1, content : 1, time : 1}; // 待返回的字段 var options = {}; mongooseModel.find(criteria, fields, options, function(error, result){ if(error) { console.log(error); } else { console.log(result); } //关闭数据库链接 db.close(); }); // 删除记录 var conditions = {username: 'emtity_demo_username'}; mongooseModel.remove(conditions, function(error){ if(error) { console.log(error); } else { console.log('delete ok!'); } //关闭数据库链接 db.close(); });