Nodejs + mongodb

Time:2021-4-17

1、 Introduction to mongodb

Mongodb is based onDistributed file storageDatabase of, byC + + languageWrite, mainly for web applications, to provide scalable high-performance data storage solutions.

Mongodb is an intermediaterelational databaseandNon relational databaseThe products between right and wrong relational databases are the most functional and most like relational databases. The data structure it supports is very loose and similarjsonOfbsonFormat, so you can store more complex data types. The most important feature of Mongo is that the query language it supports is very powerful. Its syntax is a bit similar to the object-oriented query language. It can almost realize most of the functions similar to the single table query of relational database, and it also supports the indexing of data.

Bson example:

{
    name: "MongoDB",
    type: "bson",
    data: {
        id: 1,
        code: 123456
    }
}

The differences between bson and JSON are as follows:

① Faster traversal speed
Bson will store the data length in the header for easy retrieval

② Easier to operate
If you change a number from 9 to 10, the data length will change, and all contents will be moved back one bit. Bson can specify the data column, and only the data column can be modified. However, if the data changes from integer to integer, it will also lead to the data becoming longer.

③ Additional data types have been added
Bson adds a byte array data type, which does not need to be converted to Base64 before being saved as JSON.

Advantages of Distributed Computing:
Reliability (fault tolerance)
Reliability is an important advantage of distributed computing system. A system crash on one server does not affect the rest of the servers.

Scalability:
In the distributed computing system, more machines can be added as needed.

Resource sharing:
Data sharing is essential for applications such as banking and reservation systems.

Flexibility:
Because the system is very flexible, it is easy to install, implement and debug new services.

Faster speed:
Distributed computing system can have the computing power of multiple computers, which makes it faster than other systems.

Open system:
Because it is an open system, the service can be accessed locally or remotely.

Higher performance:
Compared with the centralized computer network, cluster can provide higher performance (and better cost performance).

Disadvantages:Less software, network problems, security

What is a relational database
Relational database refers to the database that uses relational model to organize dataRows and columnsData is stored in the form of table to facilitate user understanding. A series of rows and columns in a relational database are called tables, and a group of tables make up the database. The user searches the data in the database through the query, and the query is an executive code used to limit some areas in the database. The relational model can be simply understood asTwo dimensional tableA relational database is a data organization composed of two-dimensional tables and their relationships.
Relational databases: Oracle, DB2, mysql, Microsoft SQL server, Microsoft Access, etc

What is a non relational database
It refers to non relational, distributed, data stored in the form of key value pairs, and the structure is not fixed.
Strictly speaking, it should be a collection of data structured storage methods
Disadvantages: it can only store simple data and does not support things

2、 Features of mongodb:

1. Data of easy to store object type oriented to collection
2. Model freedom
3. Support dynamic query
4. Support full index, etc

3、 Mongodb data model

Monogodb can build multiple databases, each database has its own permissions and collection, and different databases are also placed in different files.

A mongodb instance can contain a group of databases, a database can contain a group of collections, and a collection can contain a group of documents. A document contains a set of fields, each of which is a key / value pair.

Key: must be of string type.
Value: can contain the following types.
● basic types, e.g,string,int,float,timestamp,binaryAnd so on.
A document.
Array type.

4、 Nodejs

Nodejs is a JS running environment based on V8 engine, which uses event driven and non blocking I / O model to make it lightweight and efficient. V8 is the name of JavaScript engine supporting Google Chrome. When browsing with chrome, it is responsible for processing and executing JavaScript.
V8 is written in C + + language, is portable, and can run on MAC, windows, Linux and other systems

Nodejs uses the commonjs module, and the system browser uses the Es5 module standard
Nodejs uses require() and browser uses import ()

Nodejs + mongodb

var mongoClient = require('mongodb').mongoClient;
var url = 'mongodb://localhost:27017/runoob'

mongoClient.connects(url, function() {
    if (err) throw err;
    var dbData = db.db('runoob');
    //Create collection
    dbData.createCollection('site', function() {
        db.close () // close the database
    })
    //Insert data
    Var STR = {Name: "Zhang San"};
    dbData.collection('site').insertOne(str, function(err,data) {
        db.close()
    })
    //Insert multiple data insertmany ()
    var str2 = {$set: { "url" : "https://www.abc.com" }};
    //STR is the query condition
    dbData.collection('site').insertMany(str, str2, function(){
        db.close()
    })
    //Query data find ()
    //Query the specified data find ({Name: "Zhang San"})
    //Update dataupdateone ()
    //Update multiple data updatemany ()
    //Delete datadelete one ()
    //Delete multiple data
    //Sort (type) type = {type: 1} ascending - 1 descending
    //Paging find (). Limit (2)
    //Skip find (). Skip (2)
    
})