MongoDB学习(2)—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学习(2)—Node.js与MongoDB的基本连接示例的更多相关文章
- node.js和MongoDB学习网址
回想我写的node.js和MongoDB的博客,都是菜鸟教程那里的知识,所以我打算不写了,那些都是基础,在项目开发中,我发现单单有那些基础不行,这些基础可以尽快学好,然后学习项目开发过程 下面是两个教 ...
- 以太坊开发DApp实战教程——用区块链、星际文件系统(IPFS)、Node.js和MongoDB来构建电商平台(一)
第一节 简介 欢迎和我们一起来用以太坊开发构建一个去中心化电商DApp!我们将用区块链.星际文件系统(IPFS).Node.js和MongoDB来构建电商平台类似淘宝的在线电商应用,卖家可以自由地出售 ...
- [Node.js]连接mongodb
摘要 前面介绍了node.js操作mysql以及redis的内容,这里继续学习操作mongodb的内容. 安装驱动 安装命令 cnpm install mongodb 安装成功 数据库操作 因为mon ...
- Node.js+Express+MongoDB数据库实现网页注册登入功能
通过 Node.js + Express + MongoDB 实现网页注册账号 和 登入账号的功能 项目准备: 1: 事先准备好项目的页面 (首页页面 index.html)(登入页面 login.h ...
- Node.js 中MongoDB的基本接口操作
Node.js 中MongoDB的基本接口操作 连接数据库 安装mongodb模块 导入mongodb模块 调用connect方法 文档的增删改查操作 插入文档 方法: db.collection(& ...
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)
这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000005040834 突然想起来我这个博客冷落了好多年了,也该更新一下,呵 ...
- Node.js和MongoDB - MongoJS入门
第一次尝试翻译外国牛人的博文,希望大家喜欢. 本文源码详见:https://github.com/njaulj/mongojs 一点都不夸大的说,近年来node.js和mongodb的确是大放异彩,在 ...
- Node.js与MongoDB的基本连接示例
Node.js与MongoDB的基本连接示例 前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务 ...
- [Docker] Linking Node.js and MongoDB Containers
To do communcation between containers, we need to do link between containers. 1. Run a container wit ...
随机推荐
- Python之异常追踪模块:traceback
正常时输出追踪信息: import traceback def stack(): print 'The python stack:' traceback.print_stack() from twis ...
- 新建samba配置步骤
Linux系统默认已经安装了Samba,但是没有安装Samba服务: 1,先查看安装情况:rpm -qa|grep samba 根据系统的安装情况选择下载或者通过光驱安装所缺的rpm包. 我的安装情况 ...
- ios 在ios9中 NSNotificationCenter addObserver 不会影响对象释放
如题,ios9上, [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@&qu ...
- 游戏服java程序启动,显示内存溢出
1.OutOfMemoryError:Java heap space 过程:服务器上面的mysql突然异常重启,导致了程序启动的时候报错 问题1:OutOfMemoryError:Java heap ...
- PHP接入umeditor(百度富文本编辑器)
2015年6月28日 23:08:49 星期日 效果: 开搞;) 首先: 百度官网上下载 umeditor 简版的富文本编辑器(这里) 然后: 解压放到自己的项目中, 配置服务器, 保证能在浏览器端加 ...
- SQL Update 巧用
JOIN 样本 ********************************** Update 结存 set 结存.现有库存=c.入仓数-b.出仓数量 from 结存 a )) 入仓数 from ...
- BestCoder37 1001.Rikka with string 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5202 题目意思:给出一个长度为 n,只有小写字母和 ? 组成的字符串.现在需要向 ? 的位置填小写字母 ...
- javascript中元素的scrollLeft和scrollTop属性说明
再说意义之前,前说一下这两个属性的适用范围: 注意:这两个属性只能用于元素设置了overflow的css样式中.否者这两个属性没有任何意义.且overflow的值不能为visible,但可以为hidd ...
- Android笔记:四大组件
1.Activity(是用户可以看到的主要的界面,使用时需要在AndroidManifest.xml中编辑声明.) 启动模式: 启动模式一共有四种,分别是standard.singleTop.sing ...
- September 6th 2016 Week 37th Tuesday
I only wish to face the sea, with spring flowers blossoming. 我只愿面朝大海,春暖花开. That scenery is beautiful ...