Big Bear nodejs------MongoDB Module (additional)

Source: Internet
Author: User
Tags install mongodb

First, the opening analysis

This is an extended knowledge article because database operations are used in the following article, so today it is (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-based database that is a popular one in the current NoSQL database.

MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-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. MONGO's biggest feature is that the query language he supports is very powerful, and its syntax is a bit like an object-oriented query language that almost implements most of the functionality of a relational database single-table query, and also supports indexing of data.

The traditional relational database is usually composed of three hierarchical concepts of database, table, record, and MongoDB is made up of database, collection (collection), Three levels of document objects.

MongoDB is for tables in relational databases, but there is no concept of columns, rows, and relationships in the collection, which embodies the nature of pattern freedom.

(2), features

It is characterized by high performance, easy to deploy, easy to use, and easy to store data. The main features are:

1) for collection storage, easy to store the object type of data.

2) Mode freedom.

3) Support dynamic query.

4) full index support, including internal objects.

5) Support query.

6) Support replication and recovery.

7) Use efficient binary data storage, including large objects (such as video, etc.).

8) automatically handles fragmentation to support scalability at the cloud level.

9) support multiple languages such as ruby,python,java,c++,php,c#.

10) The file storage format is Bson (a JSON extension).

11) can be accessed via the network.

(3), installation and use

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

    

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

Mongod.exe------used to connect to the MONGO database server, which is the server side.

Mongo.exe------used to start the MongoDB shell, which is the client.

Second, step-by 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 is trying to access MongoDB on the native driver port. For HTTP diagnostic access, add the port number ", indicating success.

  In this case, the MongoDB database service has been successfully started.

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

    

(4), type the following command in the Shell command window: "Use BB" (used command toggles the current database, if the database does not exist, a new one is created first).

(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 ()" (Displays all data documents under the Users collection), such as:

    

Note the "_id" in the figure, and the system automatically assigns a unique primary key _id to each record.

  All right! Basic function enough, and later if additional operations need to be added gradually, because space is limited mainly said Nodejs operation MongoDB, so temporarily.

  

Third, enter the topic, case analysis

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

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

    

1 varMongoDB = require ("MongoDB") ;2 varServer =NewMongodb. Server ("localhost", 27017,{3Auto_reconnect:true4 }) ;5 varconn =NewMongodb. Db ("BB", server,{6Safe:true7 }) ;8Conn.Open (function(error,db) {9     if(Error)Throwerror;TenDb.collection ("Users",{ OneSafe:true A},function(err,collection) { -         if(ERR)Throwerr; -Collection.find (). ToArray (function(e,docs) { the             if(e)Throwe; - Console.log (docs); -         }) ;  -     }) ; +}) ;

The results of the operation are as follows:

  

12    {3        _id:54b3ce920dc20a3ba9607f4         Name: ' Bigbear ',5         Password: ' 12345678 '6    }7 ]

Four, summarize

(1), familiar with MongoDB database.

(2), understanding the difference between the relational database.

(3), through the above examples, master Nodejs how to operate MongoDB.

(4), emphasizing a sentence: How to design a "NoSQL" type of database, thinking is very important.

hahaha, the end of this article, not to be continued, hope and we have enough communication, common progress ... To shout ... (*^__^*)              

Big Bear nodejs------MongoDB Module (additional)

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.