前提

已经安装了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学习(2)—Node.js与MongoDB的基本连接示例的更多相关文章

  1. node.js和MongoDB学习网址

    回想我写的node.js和MongoDB的博客,都是菜鸟教程那里的知识,所以我打算不写了,那些都是基础,在项目开发中,我发现单单有那些基础不行,这些基础可以尽快学好,然后学习项目开发过程 下面是两个教 ...

  2. 以太坊开发DApp实战教程——用区块链、星际文件系统(IPFS)、Node.js和MongoDB来构建电商平台(一)

    第一节 简介 欢迎和我们一起来用以太坊开发构建一个去中心化电商DApp!我们将用区块链.星际文件系统(IPFS).Node.js和MongoDB来构建电商平台类似淘宝的在线电商应用,卖家可以自由地出售 ...

  3. [Node.js]连接mongodb

    摘要 前面介绍了node.js操作mysql以及redis的内容,这里继续学习操作mongodb的内容. 安装驱动 安装命令 cnpm install mongodb 安装成功 数据库操作 因为mon ...

  4. Node.js+Express+MongoDB数据库实现网页注册登入功能

    通过 Node.js + Express + MongoDB 实现网页注册账号 和 登入账号的功能 项目准备: 1: 事先准备好项目的页面 (首页页面 index.html)(登入页面 login.h ...

  5. Node.js 中MongoDB的基本接口操作

    Node.js 中MongoDB的基本接口操作 连接数据库 安装mongodb模块 导入mongodb模块 调用connect方法 文档的增删改查操作 插入文档 方法: db.collection(& ...

  6. 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)

    这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000005040834 突然想起来我这个博客冷落了好多年了,也该更新一下,呵 ...

  7. Node.js和MongoDB - MongoJS入门

    第一次尝试翻译外国牛人的博文,希望大家喜欢. 本文源码详见:https://github.com/njaulj/mongojs 一点都不夸大的说,近年来node.js和mongodb的确是大放异彩,在 ...

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

    Node.js与MongoDB的基本连接示例 前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务 ...

  9. [Docker] Linking Node.js and MongoDB Containers

    To do communcation between containers, we need to do link between containers. 1. Run a container wit ...

随机推荐

  1. ios 中的autoresizingMask

    以前对这个知识理解的不太对,看了下面这个地址的文章后,感觉说的对,也没检验,今天实验后,发现是错的...在这里对以前读过此文的朋友表示抱歉. 原文地址如下: http://www.cnblogs.co ...

  2. dump、cpio、tar、dd四种备份工具比较

    原文  http://blog.csdn.net/ether_lai/article/details/12656219 dump dump可执行文件系统增量备份的存储操作 ,dump 可将目录或整个文 ...

  3. [转] Android Volley完全解析(一),初识Volley的基本用法

    版权声明:本文出自郭霖的博客,转载必须注明出处.   目录(?)[-] Volley简介 下载Volley StringRequest的用法 JsonRequest的用法   转载请注明出处:http ...

  4. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. 关于Javascript splice方法的一个坑。

    w3c相关文档:http://www.w3school.com.cn/jsref/jsref_splice.asp bug:购物车计算价格的时候.加商品没问题,减商品的时候价格总是计算错误. 经排查发 ...

  6. UI课堂笔记

    2016.7.18 + (UIColor *)blackColor; + (UIColor *)darkGrayColor;   深灰色 + (UIColor *)lightGrayColor;  浅 ...

  7. [Android Pro] adb 进入 recovery, adb 进入 bootloader

    reference to : http://blog.csdn.net/mldxs/article/details/18699965 重启到Recovery界面 adb reboot recovery ...

  8. 解决客户端访问https报错

    现象: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure at com.sun.net.ssl. ...

  9. zmq-ios framwork

    1.附件见zeromq-ios.framework百度网盘/iOS/zmq 2.zeromq-ios.framework解压拖进工程文件 3.objc-zmq见百度网盘/iOS/zmq 4.objc- ...

  10. java 小数点处理

    public class Test { public static void main(String[] args) { double i = 3.856; // 舍掉小数取整 System.out. ...