This article mainly introduces Node in Wondows. js uses the MongoDB environment configuration. Here, the Mongoose driver is used to allow JavaScript to operate MongoDB. For more information, see a database. MongoDB and Node. js is a perfect fit, because MongoDB is a document-based non-relational database that is stored in BSON (lightweight binary format of JSON, commands for database management, such as addition, deletion, modification, and query, are similar to the JavaScript syntax. If you access MongoDB data in Node. js, we will feel like a family and very friendly.
I am also going to use MongoDB as my database.
MongoDB uses collection and document to describe and store data. collection is equivalent to a table and document is equivalent to a row. However, in a relational database such as MySQL, the table structure is fixed, for example, a row is composed of several columns, and all rows are the same. Different from MongoDB, multiple documents in a set can have different structures and are more flexible.
Install Mongo
For more information, see the following link: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows /.
Download the installation package from https://www.mongodb.org/downloads. the Windows system is an msifile, and I choose the latest Windows 64-bit 2008 R2 + version.
The installation is very simple. You can choose the installation location by default. I installed it in the MongoDB directory on the G disk. After installation, the directory structure is as follows: G: \ MongoDB \ Server \ 3.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 for it to store data. I created a folder named db under G: \ MongoDB.
Open cmd, go to the G: \ MongoDB \ Server \ 3.0 \ bin directory, and run "mongod-dbpath = G: \ MongoDB \ db" to start MongoDB. The following figure is displayed:
After MongoDB is started, it will listen on a port and wait for the client to connect. From this we can see that the default listening port is 27017. You can use the "-port" option to change this port. For example, the "mongod-port 28018-dbpath = G: \ MongoDB \ db" command will start MongoDB and listen to port 28018.
After MongoDB is started, we can use mongo (Interactive shell) to manage the database. Run mongo directly in the bin directory. You can see that:
Mongo Shell connects to the test database by default. It also tells us that you can enter help to view help. You can type help and press enter to see which commands are available.
Note: by default, mongod does not contain authentication when it is started. After the client is connected, you can perform any operations, such as creating a database, adding, deleting, modifying, and querying the database. If you want to restrict user permissions, you can configure them by yourself. I will go straight down here.
Install the mongoose driver
Install the GIT tool:
The github website does not support direct download and packaging of the source code packages of all submodules, so you need to use the git tool to check out all the source code. From the repository (the latest repository file is git-1.7.7.1-preview20111027.exe ). Double-click to install the SDK.
Download NPM source code:
Open the command line tool (CMD) and execute the following command. You can use msysgit to check out all NPM source code and dependent code and install npm.
git clone --recursive git://github.com/isaacs/npm.gitcd npmnode cli.js install npm -gf
Before executing this code, ensure that node.exe is installed using node. msi or in the path environment variable. This command will also add npm to the path environment variable, and then you can run the npm command everywhere. If you encounter permission errors during installation, make sure that the cmd command line tool runs as an administrator. After the installation is successful, run the following command:
npm install underscore
Return Value:
underscore@1.2.2 ./node_modules/underscore
In this way, the NPM installation on Windows is complete, and then we can install mongoose
npm install mongoose
Instance
Some basic operations and descriptions are written in the code comment:
// 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 the mongoose static method. The static method can use mongooseSchema at the Model layer. statics. findbytitle = function (title, callback) {return this. model ('mongoose '). find ({title: title}, callback);} // modelvar mongooseModel = db. model ('mongoose', mongooseSchema); // Add records based on enti Ty operation 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! ');} // Close the database link to db. close () ;}); // Add a record based on the 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 ');} // closes the database link to db. close () ;}); // modify the record mongooseModel. update (conditions, update, options, callback); var conditions = {username: 'model _ demo_usern Ame'}; 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 the database link to db. close () ;}); // query // var mongooseEntity = new mongooseModel ({}) based on the instance method; mongooseEntity. findbyusername ('model _ demo_username ', function (error, result) {if (error) {console. log (error);} else {console. log (result);} // closes the database link to db. close () ;}); // query mongooseModel based on static methods. findbytitle ('emtity _ demo_title ', function (error, result) {if (error) {console. log (error);} else {console. log (result );} // Close the database link to db. close () ;}); // mongoose findvar criteria = {title: 'emtity _ demo_title '}; // query condition var fields = {title: 1, content: 1, time: 1}; // The Field var options ={}; mongooseModel. find (criteria, fields, options, function (error, result) {if (error) {console. log (error);} else {console. log (result);} // closes the database link to db. close () ;}); // Delete the record var conditions = {username: 'emtity _ demo_username'}; mongooseMod El. remove (conditions, function (error) {if (error) {console. log (error);} else {console. log ('delete OK! ');} // Close the database link db. close ();});