NodeJS ------ MongoDB module (additional article)

Source: Internet
Author: User

NodeJS ------ MongoDB module (additional article)
I. The first part of this article is about extended knowledge. Because database operations will be used in the following article, let's talk about it (Mongodb module) today ). (1) Introduction MongoDB is a Database Based on Distributed File storage. 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) It features high performance, easy deployment, and ease of use, making it very convenient to store data. Main functions and features include: 1) Set-oriented storage, 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) install and use 1. Download and decompress "mongodb" to the specified directory, as follows: 2. Two of the most important files are: mongomongod.exeworks and mongomongo.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. Perform step-by-step operations (1) to 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 ", indicates that the operation is successful. By now, the MongoDB Database Service has been successfully started. (3) create a data warehouse ------, and click "cmd.exe". The following interface is displayed: (4) in the shell command window, enter the following command: "use bb" (use command to switch to the current database, if the database does not exist, 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 data entry into the users set. If the users set does not exist, A new one will be created first, and then the data will be inserted. The parameter is passed in 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 that in the figure" _ id ", 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. Enter the topic, instance analysis (1), and npm install mongodb (download and put it under the specified directory ). (2) We have already created databases and collections ("bb", "users") 1 var mongodb = require ("mongodb"); 2 var server = new mongodb. server ("localhost", 27017, {3 auto_reconnect: true 4}); 5 var conn = new mongodb. db ("bb", server, {6 safe: true 7}); 8 conn. open (function (error, db) {9 if (error) throw error; 10 db. collection ("users", {11 safe: true12}, function (err, collection) {13 if (err) throw err; 14 collection. find (). toArray (fu Nction (e, docs) {15 if (e) throw e; 16 console. log (docs); 17}); 18}); 19}); the running result is as follows: 1 [2 {3 _ id: 54b3ce920dc20a3ba9607f4 name: 'biginar ', 5 password: '000000' 6} 7] 4. To sum up, (1) be 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.