Node.js与MongoDB的基本连接示例

前提

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

初始化数据

启动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})

 
分类: MongoDB

最新文章

  1. 在Azure虚拟机上安装SQL server
  2. UIPickerView的使用(三)
  3. android adb pull push
  4. mapreduce出现类似死锁情况
  5. eclipse修改工程名
  6. POJ No.3680 Intervals
  7. nyoj 27 水池数目
  8. bzoj1819
  9. (原)10-folder交叉验证
  10. Prepare Python environment and install selenium.
  11. 打印Java异常堆栈信息
  12. HTML-Note
  13. Codeforces 1009G Allowed Letters 最大流转最小割 sosdp
  14. mysql系列十二、mysql常用hint
  15. OSGI引入Spring DM实现对服务对象的管理
  16. scheme 教程 #lang racket
  17. python_不用循环打印1-1000
  18. node、npm的安装和环境变量的配置
  19. python基础--logging模块
  20. 关于mybatis中一级缓存和二级缓存的简单介绍

热门文章

  1. openSUSE 国内镜像摘要
  2. 在Sql中使用Try Catch
  3. plsql dev中Dynamic Performance Tables not accessible分析解决(转)
  4. target-action传值
  5. Android Studio 快捷键整理分享
  6. POJ 1141 区间DP
  7. lodoop打印控制具体解释
  8. Linux概念架构的理解(转)
  9. .NET单元测试艺术(2) - 第一个单元测试
  10. [模拟] hdu 4452 Running Rabbits