MongoDB Learning (2)-node.js basic connection with MongoDB example

Source: Internet
Author: User
Tags install mongodb mongodb example

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.