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.