General Introduction:
Because the MongoDB database supports the access of the Bson object (the binary form of the JSON object) in the JavaScript scripting environment, the efficiency of accessing the data is very high. In the MongoDB database, each data record waiting to be inserted is stored in memory, so The database is a non-blocking database, which can achieve satisfactory results when it is necessary to record large amount of log data, real-time measurement data or real-time statistic data. For MONGODB database support the use of JavaScript functions within query Statements also greatly enhances its ability to read data. In addition , the MongoDB database is a document-oriented database that allows users to store child records in the parent record.
Connect to MongoDB:
A server object that represents the server on which the MongoDB database resides is created to specify the servers on which the MongoDB database needs to be connected.
var server=new MONGO. Server (Host,port,[options]);
Host: A string that specifies the address where the server resides.
PORT: A positive value that specifies the server port number.
The options are valued as follows:
SSL: Boolean. True indicates that an SSL security protocol-based connection is established between the client and server side, and the SSL security protocol needs to be enabled on the server side. The default property value is False.
Sslvalidate: Boolean value. Specifies whether the server validates the certificate submitted by the client. True, the server side needs to use SSL security protocol version 2.4 above. The default is Fasle.
SSLCA: Array. Each element in the array is a buffer object or a string. Used to specify a set of certificates for server-side authentication (the server side needs to use SSL security protocol version 2.4 above), and the default property value is null.
Sslcert: The property value is a buffer object or a string that specifies a certificate to use for server-side authentication (the server side needs to use SSL security protocol version 2.4 above), and the default property value is null.
Sslkey: The property value is a buffer object or a string that specifies a private key to be used for server-side authentication (the server side needs to use SSL security protocol version 2.4 above), and the default property is null.
Sslpass: The property value is a buffer object or a string that specifies a certificate password to use for server-side authentication (the server side needs to use SSL security protocol version 2.4 above), and the default property is null.
Poolsize: The attribute value is an integer that specifies the maximum number of connections in the connection pool. The default property is 5.
Socketoptions: The property value is an object that specifies the option to use for the port to which the server is connected, the default property is null, and the properties that can be used in the property value object are as follows.
KeepAlive: The attribute value is an integer, in milliseconds, that specifies how often the client sends a KeepAlive probe packet to the server side.
CONNECTTIMEOUTMS: The attribute value is an integer in milliseconds that specifies the client connection time-out.
SOCKETTIMEOUTMS: The attribute value is an integer in milliseconds that specifies the client port time-out.
Logger: The object used for logging, the default property value is null.
Auto_reconnect: Boolean value, True indicates that the connection is automatically rebuilt when an error occurs during a client-to-server connection. The default property is False.
Disabledriverbsonsizecheck: Property value is a Boolean value, when the property value is true, forcing the server side to throw an error when the Bson object size is too large, the default property is False.
After a successful creation of the MongoDB server object, you need to create a DB object that represents the MongoDB database.
var db=new MONGO. Db (Databasename,server,[options]);
DatabaseName: String, database name.
Server: The Servers object where the database resides.
Options parameters:
W: the attribute value is an integer greater than-1 or a string. This property value is used to set the write concern mechanism inside the MONGODB database when writing data operations, using write inside the MongoDB database The concern mechanism to report whether a write operation for a data is successful. When the W attribute value is an integer less than 1, the write concern mechanism does not recognize that one data is written, and when the W attribute value is greater than the integer value or string that is equal to 1, the write concern mechanism admits that a data is written .
Wtimeout: The property value is an integer value that specifies the time-out when the write data operation occurs. The unit is in milliseconds.
Fsync: Boolean value. Specifies whether to wait for the fsync operation to be used internally by the MongoDB database before the method of the Write database operation returns (the operation writes the remaining pending data to the database all). The default property value is False.
Journal: Boolean value whether to wait for the journal operation, which is written to the execution log in the database, to end before the method of the write database operation returns. The default property value is False.
Native_parser: The property value is a Boolean value. The C + + Bson parser is used inside the database. The default property value is False.
Forceserverobjectid: Boolean value, whether the Bson object ID is created on the server side, rather than on the client. The default property value is False.
Pkfactory: The object overloads the database internally generated object ID primary key.
Serializefunctions: The property value is a Boolean value. Specifies whether to serialize JavaScript functions within the database. The default property value is False.
Raw: Use the Bson data buffer within the database to perform data access operations. The default property value is False.
Recordquerystats: Specifies whether query statistics are to be executed within the database when querying data. The default property is False.
Retrymiliseconds: Integer in milliseconds. Specifies how often to retry the connection when the connection database operation fails. The default is 5000.
Numberofretries: Specifies the number of times to reconnect the database when the database connection fails. Default is 5.
Logger: The object that records the action log, the default property value is null.
Slaveok: Integer value. Sets the Slaveok value that is used internally in the database when the query is set (only valid when a specified connection to a secondary server needs to be displayed), and defaults to null.
Safe: Boolean. True means that data access is performed using the GetLastError command. This command returns the execution result of the access operation. The default property value is False.
After the DB object is created, the database connection operation needs to be performed using the object's Open method.
Db.open (function (err,db) {});
In the callback function, the Err parameter indicates the error that was thrown when the database connection failed. DB represents the DB object after a successful connection. If the connection fails, the value is null.
When the database is not in use, the data can be used to close the database using the Close method of the object.
Db.close ([Closetype],[callback]);
Closetype: When True, forces the database to shut down and no longer open the database with the open method of the database object. When False, the database is not forced to be closed. When the database is closed, you can open it by using opening.
Callback:function (Err) {}, after the action is closed.
Node Connection MongoDB article