MongoDB學習筆記(2)—Node.js與MongoDB的基本串連樣本

來源:互聯網
上載者:User

MongoDB學習筆記(2)—Node.js與MongoDB的基本串連樣本
前提

已經安裝了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0。

MongoDB學習筆記(1)—在Windows系統中安裝MongoDB 

如何在CentOS 7安裝Node.js

Ubuntu 14.04下搭建Node.js開發環境 

Ubunru 12.04 下Node.js開發環境的安裝配置

Node.Js入門[PDF+相關代碼]

Node.js開發指南 高清PDF中文版 +源碼

初始化資料

啟動MongoDB服務,在test資料庫中插入一條執行個體資料:

db.user.install({name:"scaleworld",age:27});
在Node.js中引入MongoDB模組
npm install mongodb
編寫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);            }        });    });});
運行
{ [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 receives does not                ==   return the information of the success/error of the insert/update/remove            ==                                                                                      ==   ex: new Db(new Server('localhost', 27017), {safe:false})                           ==                                                                                      ==   http://www.mongodb.org/display/DOCS/getLastError+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
 
雖然最後列印出了我們之前插入的資料,但是前面一大串的錯誤還是人看著不舒服,我們要消滅它們。
Error: Cannot find module '../build/Release/bson'的解決辦法
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }js-bson: Failed to load c++ bson extension, using pure JS version

頭兩行說的是沒有發現bson模組。好辦我們立馬安裝:

npm install bson

然後將D:\nodejsdemo\node_modules\mongodb\node_modules\bson\ext\index.js中的bson = require('../build/Release/bson')改成bson = require('bson') ,重新運行。

不過這樣只是解決頭兩行的問題,還有用=包圍起來的問題。

“Please ensure that you set the default write concern for the database”的解決辦法

從最後一句話“This message will disappear when the default safe is set on the driver Db”我們就可以看出該問題的解決辦法,只要將資料庫連接設定為安全即可。

具體代碼修改如下:

new mongodb.Db('test',server,{})修改為new mongodb.Db('test',server,{safe:true})

Ubuntu 13.04下安裝MongoDB2.4.3

MongoDB入門必讀(概念與實戰並重)

Ubunu 14.04下MongoDB的安裝指南

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios監控MongoDB分區叢集服務實戰

基於CentOS 6.5作業系統搭建MongoDB服務

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

本文永久更新連結地址:

相關文章

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.