The code is easy to understand, but I've wasted a lot of time in the process, and I've seen a small hole in the Nodejs async. There are many pits in the future, so find the pit pits slowly.
The resources are as follows:
1. Assertion module: https://nodejs.org/api/assert.html
2. MongoDB module: https://github.com/mongodb/node-mongodb-native
Needless to say, after sending the code to sleep, interested friends can continue to pay attention to this series.
1 //loading the MongoDB module in Nodejs2 varMongoclient = require (' MongoDB '). mongoclient;3 4 //load assert (Assert module) reference address: https://nodejs.org/api/assert.html5 varassert = require (' Assert ');6 7 //the MongoDB host address test represents the database that is currently located8 varurl = ' Mongodb://localhost:27017/test ';9 //Start the MongoDB service, establish the connectionTenMongoclient.connect (URL,function(err, db) { OneAssert.equal (NULL, err); AConsole.log ("Connected correctly to server"); - - //How to set up synchronous nesting theInsertdocuments (DB,function() { -UpdateDocument (DB,function() { -Deletedocument (DB,function() { -Finddocuments (DB,function() { + db.close (); - }); + }); A }); at }); - - //carefully observe the curd execution order of synchronous and asynchronous. Attempt to add db.close () after async; Think about why the error. - //the asynchronous Notation - //insertdocuments (db, Function () {}); - //updatedocument (db, Function () {}); in //deletedocument (db, Function () {}); - //finddocuments (db, Function () {}); to + - the * }); $ Panax Notoginseng - the //The following shows the operation of Curd + varInsertdocuments =function(DB, callback) { A //get a collection of documents the varCollection = Db.collection (' rover ')); + //Construct Data - varTestData = [{A:1},{a:2},{a:3}]; $ //Inserting Data $Collection.insertmany (TestData,function(err, result) { -Assert.equal (Err,NULL); -Assert.equal (3, RESULT.RESULT.N); theAssert.equal (3, result.ops.length); -Console.log (' Inserted 3 Documents into the document collections ');Wuyi callback (result); the - }); Wu - }; About $ - //Updating a Documents modification operation - varUpdateDocument =function(DB, callback) { - //get a collection of documents A varCollection = Db.collection (' rover ')); + //Modify a Document collection the varUpdate = {A:2}; - varChange = {$set: {a:5555}}; $Collection.updateone (Update,change,function(err, result) { theAssert.equal (Err,NULL); theAssert.equal (1, RESULT.RESULT.N); theConsole.log ("Updated the document with the field a equal to 2"); the callback (result); - }) in }; the the //Delete a document About varDeletedocument =function(DB, callback) { the //Get The Documents collection the varCollection = Db.collection (' rover ')); the //Insert Some documents +Collection.deleteone ({a:3},function(err, result) { -Assert.equal (Err,NULL); theAssert.equal (1, RESULT.RESULT.N);BayiConsole.log ("removed the document with the field a equal to 3"); the callback (result); the }); - }; - the //reading Data the varFinddocuments =function(DB, callback) { the //Get The Documents collection the varCollection = Db.collection (' rover ')); - //Find Some documents theCollection.find ({}). ToArray (function(Err, docs) { the //assert.equal (err, null); the //assert.equal (2, docs.length);94Console.log ("Found The following records"); the Console.dir (docs); the callback (docs); the });98};
NodeJs + MongoDB Module Demo