NodeJS learning notes-MongoDB module _ node. js

Source: Internet
Author: User
Tags install mongodb mongodb driver
Nodejs is a powerful platform with basic functions and many modules. We now need to connect to the mongodb driver, just like mysql's java driver. Nodejs has several third-party drivers for mongodb. Unlike jdbc, there is no standard. Therefore, you need to go to the drive website to learn how to use it to access mongodb. I. Opening Analysis

This is an extended knowledge article, because database operations will be used in the following article, so today we will talk about it (Mongodb module ).

(1 ),Introduction

MongoDB is a distributed file storage-based database. Written in C ++. It is designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a high-performance, open-source, and non-pattern document-based database. It is a popular NoSql database.

MongoDB is a product between relational databases and non-relational databases. It has the most abundant functions and features like relational databases. The supported data structure is very loose and is similar to the json bjson format. Therefore, it can store complicated data types. The biggest feature of Mongo is that it supports a very powerful query language. Its syntax is somewhat similar to an Object-Oriented Query Language. It can almost implement most of the functions similar to single-table queries in relational databases, it also supports data indexing.

Traditional relational databases are generally composed of three levels: database, table, and record. MongoDB is composed of databases and collections) and document objects.

MongoDB has no concept of columns, rows, and links for tables in relational databases, which reflects the free mode.

(2 ),Features

It features high performance, ease of deployment, and ease of use, making it easy to store data. Features:

1) oriented to centralized storage, it is easy to store object-type data.
2) Free mode.
3) supports dynamic query.
4) supports full indexing, including internal objects.
5) query is supported.
6) supports replication and fault recovery.
7) use efficient binary data storage, including large objects (such as videos ).
8) automatically process fragments to support scalability at the cloud computing level.
9) supports RUBY, PYTHON, JAVA, C ++, PHP, C #, and other languages.
10) the file storage format is BSON (a json extension ).
11) accessible through the network.

(3 ),Installation and Use

1. Download and decompress "mongodb" to the specified directory, as shown below:

2. There are two most important files: zookeeper d.exegion and zookeeper .exe ".

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

2. Step-by-Step operations

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

(2) Open the browser and enter "http: // 127.0.0.1: 27017/". The following words are displayed:

"You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number", indicating that the operation is successful.

By now, the MongoDB Database Service has been successfully started.

(3rd, create a data warehouse ------import, and click "command" cmd.exe ". The following page is displayed:

(4) In the shell command window, type the following command: "use bb" (use command is used to switch to the current database. If the database does not exist, a new one will be created first ).

(5) In the shell command window, type the following command: "db. users. insert ({" name ":" bigbear "," password ":" 12345678 "})",

(This command inserts a piece of data into the users set. If the set users does not exist, a new one is created first, and then the data is inserted. The parameter is passed in as JSON format ).

(6) In the shell command window, type the following command: "db. users. find ()" (displays all data documents in the users set), for example:

Note the "_ id" in the figure. The system automatically assigns a unique primary key _ id to each record.

Okay! The basic functions are enough. If additional operations are required later, the node. JS is used to operate Mongodb due to the limited space, so it will come to an end temporarily.

3. Go to the topic and perform instance analysis.

(1), npm install mongodb (download and put it in the specified directory ).

(2) We have already created databases and collections ("bb", "users ")

The Code is 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 running result is as follows:

The Code is as follows:


[
{
_ Id: 54b3ce920dc20a3ba9607f
Name: 'biginar ',
Password: '000000'
}
]

Iv. Summary

(1) familiar with MongoDB databases.
(2) understanding the differences with relational databases.
(3) Use the above example to learn how NodeJS operates MongoDB.
(4) The idea of designing a "NoSQL" database is very important.

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.