MongoDB Study Notes (2)-basic connection example between Node. js and MongoDB

Source: Internet
Author: User
Tags install mongodb
Node. js and MongoDB have been installed. The node. js used in this article is v0.12.0 and MongoDB is 3.0.0.

Node. js and MongoDB have been installed. The node. js used in this article is v0.12.0 and MongoDB is 3.0.0.

Prerequisites

Node. js and MongoDB have been installed. The node. js used in this article is v0.12.0 and MongoDB is 3.0.0.

MongoDB Study Notes (1)-install MongoDB on Windows

How to install Node. js in CentOS 7

Build a Node. js development environment in Ubuntu 14.04

Install and configure the Node. js development environment in javasru 12.04

Getting started with Node. Js [PDF + related Code]

Node. js Development Guide hd pdf Chinese version + source code

Initialize data

Start the MongoDB service and insert an instance data record to the test database:

Db. user. install ({name: "scaleworld", age: 27 });

Introduce MongoDB module in Node. js

Npm install mongodb

Compile mongodbDemo. js

Var mongodb = require ('mongodb '); var server = new mongodb. server ("localhost", 27017, {safe: true}); new mongodb. db ('test', server ,{}). open (function (error, client) {if (error) throw error; var collection = new mongodb. 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 ======================================== ========================================================== ==== Please ensure that you set the default write concern for the 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 ==== For backward compatibility safe is still supported and = allows values of [true | false | {j: true} | {w: n, wtimeout: n} | {fsync: true}] = the default value is false which means the driver has es does not = return the information of the success/error of the insert/update/remove ==== ex: new Db (new Server ('localhost', 27017), {safe: false}) ==== http://+Command==== The default of no acknowlegement will change in the very near future === = This message will disappear when the default safe is set on the driver Db ====== ========================================================== ================================================== name: scaleworld age: 27

Although we finally printed the data we inserted earlier, the previous series of errors are still uncomfortable. We need to eliminate 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

In the first two lines, no bson module was found. We can install it immediately:

Npm install bson

Then, set D: \ nodejsdemo \ node_modules \ mongodb \ node_modules \ bson \ ext \ index. bson = require ('.. /build/Release/bson ') change to bson = require ('bson') and run it again.

However, this only solves the problem of the first two rows, and is surrounded by =.

"Please ensure that you set the default write concern for the database" solution

From the last sentence "This message will disappear when the default safe is set on the driver Db", we can see the solution to This problem, as long as the database connection is set to secure.

Modify the Code as follows:

Change new mongodb. Db ('test', server, {}) to new mongodb. Db ('test', server, {safe: true })

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

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.