Inserting data into the MongoDB database
One: Connection access collection;
In a MongoDB database, data is stored in many data collections, and a collection can be accessed using the collection method of the Database object. The method is used as follows:
Db.collection (CollectionName, [options], [callback]);
The collectionname parameter value is a string that specifies the name of the collection that needs to be accessed, which is required.
The options parameter value is an object that specifies the option to use when accessing the collection, using a property that can be searched under Baidu, with more options, and can be checked when used.
The callback parameter is used to specify the callback function at the end of the execution of the Access collection operation, as specified by the callback function:
function (Err, collection) { }
In this callback function, you can use two parameters, the first parameter value is the error object that is thrown when the Access object operation fails, the second parameter value is a collection object, represents the collection that is accessed, and the parameter value is NULL when the collection fails.
Two: Collection Object Insert method
We can insert a data document into the collection using the Insert method of the collection object, where each piece of data is a data document. The method is used as follows:
Collection.insert (docs, [options], [callback]);
In the Insert method, there are 3 parameters, and docs is the required parameter, which is a JSON object or an array of JSON objects that specifies the data document that needs to be inserted.
The options parameter is an object that specifies the option to use when inserting data, which can be under Baidu, with a bit more options, not described here.
The callback parameter specifies the callback function at the end of the execution of the Insert data operation, as follows:
function (Err, Docs) {};
In this callback function, you can use two parameters, the first parameter is the error object thrown when the insert data operation fails, the second parameter value is a JSON object or an array consisting of a JSON object, represents the inserted data document, and when the insert data operation fails, the parameter value is null.
The following inserts a data document into the Users collection to the database. When the data document is successfully inserted, output the contents of the data document in the console and close the database connection, as in the following code:
Const MONGO = require (' MongoDB '); Const Server=MONGO. Server;const Db=MONGO. Db;const Server=NewServer (' localhost ', ' 27017 ', {auto_reconnect:true}); Const DB=NewDb (' datadb ', server, {safe:true});d B.open (function(err, db) {if(err) {Throwerr; } Else{Console.log (' Successfully established database connection '); Db.collection (' Users ',function(Err, collection) {Collection.insert ({userName:' Air wise ', age:30},function(Err, docs) {if(err) {Throwerr; } Else{console.log (docs); Db.close (); } }) }); }});
As shown in the following:
Inserting data into the MongoDB database