Detailed description of Node. js in Wondows using the MongoDB environment configuration, node. jsmongodb

Source: Internet
Author: User
Tags mongodb driver mongodb server mongo shell

Detailed description of Node. js in Wondows using the MongoDB environment configuration, node. jsmongodb

A database is usually required to store Website user data and business data. 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 mongoose static method, the static method can be used in the Model layer
mongooseSchema.statics.findbytitle = function (title, callback) {
  return this.model ('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_content'};
var mongooseEntity = new mongooseModel (doc);
mongooseEntity.save (function (error) {
  if (error) {
    console.log (error);
  } else {
    console.log ('saved OK!');
  }
  // Close the database link
  db.close ();
});
// Add 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 the database link
  db.close ();
});
// modify record
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 the database link
  db.close ();
});
// Inquire
// Query based on instance method
var mongooseEntity = new mongooseModel ({});
mongooseEntity.findbyusername ('model_demo_username', function (error, result) {
  if (error) {
    console.log (error);
  } else {
    console.log (result);
  }
  // Close the database link
  db.close ();
});
// Query based on static method
mongooseModel.findbytitle ('emtity_demo_title', function (error, result) {
  if (error) {
    console.log (error);
  } else {
    console.log (result);
  }
  // Close the database link
  db.close ();
});
// mongoose find
var criteria = {title: 'emtity_demo_title'}; // query conditions
var fields = {title: 1, content: 1, time: 1}; // fields to be returned
var options = {};
mongooseModel.find (criteria, fields, options, function (error, result) {
  if (error) {
    console.log (error);
  } else {
    console.log (result);
  }
  // Close the 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 the database link
  db.close ();
});


Articles you may be interested in:
  • Basic tutorial on installing and using Mongoose with Node. js for MongoDB operations
  • Install Node. js and mongodb notes on CentOS
  • Node. js and MongoDB implement a simple Log Analysis System
  • Node. js mongodb Operations Learning Summary
  • Address book based on AMAP location developed by AngularJS + Node. js + MongoDB
  • Interaction between mongodb databases with amazing node. js Reading Notes
  • Getting started with Node. js, Express, Ejs, Mongodb server and application development
  • Node. js instance for mongoDB operations
  • Using mongoskin in Node. js to operate mongoDB instances
  • Node. js MongoDB driver Mongoose basic usage tutorial


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.