The MongoDB module of Nodejs Learning notes _node.js

Source: Internet
Author: User
Tags install mongodb mongodb

First, the opening analysis

This is an extended knowledge article, because database operations are used in the following articles, so today it is said (MongoDB module).

(1), introduction

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for Web applications.

MongoDB is a high-performance, open source, modeless document database, is the current NoSQL database is a more popular one.

MongoDB is a product between relational database and non relational database, and is the most powerful and relational database in the relational database. The data structure he supports is very loose and is a JSON-like Bjson format, so you can store more complex data types. The biggest feature of MONGO is that the query language he supports is very powerful, and its syntax is somewhat similar to an object-oriented query language, which can almost achieve most of the functions of a single table query like relational database, and also supports indexing of data.

Traditional relational databases are generally composed of databases (database), tables (table), records (record) three levels of concepts, MongoDB by Database, collection (collection), Documents object (document) consists of three levels.

MongoDB for tables in relational databases, but there is no column, row, and relationship concept in the collection, which embodies the characteristics of pattern freedom.

(2), features

It is characterized by high-performance, easy to deploy, easy to use, storage data is very convenient. The main functional characteristics are:

1) oriented to collection storage, easy to store data of object type.
2 mode freedom.
3) Support dynamic query.
4 supports full indexing, containing internal objects.
5) Support query.
6) Support for replication and failure recovery.
7 Use efficient binary data storage, including large objects (such as video).
8 automatically handles fragmentation to support the scalability of the cloud computing hierarchy.
9 Support ruby,python,java,c++,php,c# and many other languages.
10 The file storage format is Bson (an extension of JSON).
11 can be accessed through the network.

(3), installation and use

1, download and extract "MongoDB" to the specified directory, as follows:

2, of which there are two most important documents: "Mongod.exe" and "Mongo.exe".

Mongod.exe------used to connect to the MONGO database server, that is, the server side.
Mongo.exe------used to start the MongoDB shell, that is, the client.

Second, step-by-step operation

(1), create a new directory, for example: ">mongod-dbpath data/db".

(2), open the browser input: "http://127.0.0.1:27017/", see the following words:

"You are are trying to access MongoDB on the native driver port. For HTTP diagnostic access, add 1000 to the port number, indicates success.

By this, the MongoDB database service has been successfully started.

(3), create the database------into CMD, type the command "Mongo.exe" appears as follows:

(4), in the Shell command window type the following command: "Use BB" (used to switch the current database, if the database does not exist, you will first create a new one).

(5), type the following command in the Shell command window: "Db.users.insert ({" Name ":" Bigbear "," Password ":" 12345678 "})",

(This command inserts a piece of data into the Users collection, and if the collection users do not exist, a new one is created before the data is inserted, and the parameters are passed in JSON format).

(6), type the following command in the Shell command window: "Db.users.find ()" (Display all data documents under the Users collection), as shown in the following figure:

Note that the "_id" in the graph automatically assigns a unique primary key _id to each record.

All right! The basic function is enough, if need additional operation to supplement gradually, because space is limited mainly say is Nodejs operation MongoDB, so temporarily end.

Three, into the subject, the case analysis

(1), NPM install MongoDB (after downloading into the development directory).

(2), before we have established the database and collection ("BB", "Users")

Copy Code code as follows:



var MongoDB = require ("MongoDB");


var server = new MongoDB. Server ("localhost", 27017,{


Auto_reconnect:true


}) ;


var conn = new MongoDB. Db ("BB", server,{


Safe:true


}) ;


Conn.Open (function (error,db) {


if (error) throw error;


Db.collection ("Users", {


Safe:true


},function (err,collection) {


if (err) throw err;


Collection.find (). ToArray (function (E,docs) {


if (e) throw e;


Console.log (Docs);


}) ;


}) ;


}) ;


The results of the operation are as follows:

Copy Code code as follows:

[
{
_id:54b3ce920dc20a3ba9607f
Name: ' Bigbear ',
Password: ' 12345678 '
}
]

Four, sum up

(1) Proficiency in the use of MongoDB database.
(2) To understand the differences between relational databases.
(3), through the above examples, master Nodejs how to operate MongoDB.
(4), emphasize a word: How to Design "NoSQL" type of database, thought is very important.

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.