Detailed wondows the Node.js use MONGODB environment configuration _node.js

Source: Internet
Author: User
Tags instance method mongodb git client git clone mongo shell

In order to save user data and business data for a Web site, a database is usually required. MongoDB and Node.js are particularly well matched, because MongoDB is a document-neutral database, the document is stored in Bson (the lightweight binary format of JSON), and the commands for managing databases, such as adding and deleting, are similar to JavaScript syntax. If you visit MongoDB's data in Node.js, there will be a family feeling, especially cordial.

I'm also going to use MongoDB as my database.

MongoDB uses collections (collection) and documents (document) to describe and store data, collection is equivalent to a table, document is the same as a row, but a relational database such as MySQL, table structure is fixed, such as a row by several columns , row and row are the same, and MongoDB different, multiple documents in a collection can have different structures and be more flexible.

Install MONGO

A detailed guide is here (MongoDB's Official document): https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/.

To the Https://www.mongodb.org/downloads download installation package, the Windows system is an MSI file, and I chose the "Windows 64-bit 2008 r2+" version.

Installation is very simple, you can default, you can also choose the installation location, I mounted to the G-Disk MongoDB directory. After installation, the directory structure is this: G:\MongoDB\Server\3.0\.

Mongod, MONGO, and other tools are in the Bin directory under the 3.0 directory.

Start

To use MongoDB, you need to specify a folder to hold the data, and I set up a folder named DB under G:\MongoDB.

Open cmd, enter G:\MongoDB\Server\3.0\bin directory, execute "mongod–dbpath=g:\mongodb\db", will start MongoDB, see the following figure:

When MongoDB is started, it listens on a port to wait for the client to connect, and as you can see from the image above, the default listener port is 27017. You can change the port with the "–port" option, such as the "Mongod–port 28018–dbpath=g:\mongodb\db" command to start MongoDB and listen for Port 28018.

With MongoDB started, we can use the MONGO (interactive shell) to manage the database. To execute MONGO directly in the bin directory, you can see the following figure:

The MONGO shell is connected to the test database by default, and also tells us that you can enter help to view it. You can type help and return to see what commands are available.

Note that the Mongod default startup without authentication, the client connected to the operation can be casually, build a library, add a check, such as all can be. If you want to restrict user rights, you can configure yourself, I will go straight down here.

Install Mongoose Driver

To install the Git tool:
Since the GitHub Web site does not support the direct download package of all the submodule source packages, it is necessary to check out all the source code through the Git tool. From Http://code.google.com/p/msysgit/downloads/list, You can download to msysgit the GIT client tool under this Windows platform (the latest version file is Git-1.7.7.1-preview20111027.exe). Double-click the install after downloading.

Download NPM Source:
Open the command-line tool (CMD) and execute the following command to check out all of NPM's source and dependent code and install NPM via Msysgit.

git clone--recursive git://github.com/isaacs/npm.git
CD NPM
node cli.js install NPM-GF

Before you execute this code, make sure that the Node.exe is installed in a node.msi manner, or in the PATH environment variable. This command also adds NPM to the PATH environment variable, where NPM commands can be executed everywhere. If you encounter permissions errors in your installation, make sure that the CMD command-line tool is running as an administrator. After the installation is successful, execute the following command:

NPM Install underscore

Return:

underscore@1.2.2./node_modules/underscore

So, NPM is installed under the Windows platform, and then we can install Mongoose

NPM Install Mongoose 

Instance
Some basic operations, the instructions are written in the code comments:

Mongoose link var mongoose = require (' Mongoose '); 
var db = mongoose.createconnection (' Mongodb://127.0.0.1:27017/nodejs ');
Link Error db.on (' Error ', function (Error) {Console.log (error);}); Schema structure var mongooseschema = new Mongoose.   Schema ({username: {type:string, default: ' Anonymous user '}, Title: {type:string}, content: {type:string}, time
: {type:date, Default:Date.now}, age: {Type:number}}); Add Mongoose instance Method MongooseSchema.methods.findbyusername = function (username, callback) {return This.model (' Mongoose ')
. Find ({Username:username}, callback); }//Add Mongoose static method, static method can use MongooseSchema.statics.findbytitle = function (title, callback) {return This.model in the model layer
(' Mongoose '). Find ({Title:title}, callback);
//model var Mongoosemodel = Db.model (' Mongoose ', Mongooseschema); Add record based on entity operation var doc = {username: ' emtity_demo_username ', title: ' Emtity_demo_title ', content: ' Emtity_demo_con
Tent '}; var mongooseentity = new Mongoosemodel (doc);
  Mongooseentity.save (function (Error) {if () {Console.log (error);
  else {console.log (' saved ok! ');
//Close Database link db.close ();
}); Increase record based on model operation 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 ');
//Close Database link db.close ();
});
Modify records 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! ');
//Close Database link db.close ();
});
Query//instance-based query var mongooseentity = new Mongoosemodel ({}); Mongooseentity.findbyusername (' Model_demo_username ', function (error, result) {if (error) {COnsole.log (Error);
  else {console.log (result);
//Close Database link db.close ();
}); 
  static method based query Mongoosemodel.findbytitle (' Emtity_demo_title ', function (error, result) {if (error) {Console.log (error);
  else {console.log (result);
//Close Database link db.close ();
}); Mongoose Find var criteria = {title: ' Emtity_demo_title '}; Query conditions var fields = {title:1, content:1, time:1};
The field to be returned var options = {};
  Mongoosemodel.find (Criteria, fields, options, function (error, result) {if (error) {Console.log (error);
  else {console.log (result);
//Close Database link db.close ();
});
Delete record var conditions = {username: ' emtity_demo_username '};
  Mongoosemodel.remove (conditions, function (error) {if (error) {Console.log (error);
  else {console.log (' delete ok! ');
//Close Database link db.close ();
 });

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.