Premise
node. js and MongoDB are already installed, and the node. JS used in this article is V0.12.0,mongodb 3.0.0.
Initializing data
Start the MongoDB service and insert an instance data into the test database:
Db.user.install ({name: "Scaleworld", age:27});
Introducing the MongoDB module in node. js
NPM Install MongoDB
Writing Mongodbdemo.js
varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server ("localhost", 27017,{safe:true});NewMongodb. Db (' Test ', server,{}). Open (function(error,client) {if(Error)Throwerror; varCollection =NewMongodb. Collection (client, ' user '); Collection.find (function(error,cursor) {Cursor.each (function(error,doc) {if(DOC) {Console.log ("Name:" +doc.name+ "Age:" +doc.age); } }); });});
Run
{[Error:cannot find module '. /build/release/bson '] Code: ' Module_not_found '}js-bson:failed to load C + +bson extension, using pure JS version================================================================================ensure that you set thedefaultWrite concern forThe database by setting = = one of the options = = = = W: (Value of >-1 or the string ' Majority '), where < 1 means = = no write acknowlegement = = Journal:true/false, wait for Flush to Journal before acknowlegement = = Fsync:true/false, wait for flush to file system before Acknowlegement = = = = Backward compatibility safe is still supported and = = allows values of [true|false| {j:true} | {w:n, wtimeout:n} | {fsync:true}] = = thedefaultValue isfalseWhich means the driver receives does not = =returnThe information of the success/error of the Insert/update/remove = = = = Ex:NewDb (NewServer (' localhost ', 27017), {safe:false}) == == http//Www.mongodb.org/display/DOCS/getLastError+Command == = = ThedefaultOf no acknowlegement would changeinchThe very near future = = = = This message would disappear when thedefaultSafe is set on the driver Db =================================================================================== ======Name:scaleworld Age:27
Although we finally printed out the data we inserted before, but the previous large list of errors or people looking uncomfortable, we want to destroy them.
Error:cannot Find module '. /build/release/bson ' Solution
{[Error:cannot find module '. /build/release/bson '] Code: ' Module_not_found ' }js-bson:failed to load C + + Bson extension, using pure JS version
The first two lines say that no Bson module was found. Okay, we'll install it right away:
NPM Install Bson
Then the Bson in D:\nodejsdemo\node_modules\mongodb\node_modules\bson\ext\index.js = require ('.. /build/release/bson ') changed to Bson = require (' Bson ') and rerun.
But this is only the first two lines of the problem, but also useful = surrounded by the problem.
"Please ensure the solution to the default write concern for the database"
From the last word "this message would disappear when the default safe was set on the driver Db" We can see the solution to this problem as long as the database connection is set to secure.
The specific code is modified as follows:
New MongoDB. Db (' Test ', server,{}) is modified to new MongoDB. Db (' Test ', server,{safe:true})
MongoDB Learning (2)-node.js basic connection with MongoDB example