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 ...
随机推荐
- K3数据库表结构
K3数据库表结构查看方法,直接在数据库中打开表 t_TableDescription,其中即各表及其与K3功能的对应关系 也可直接查询: select * from t_TableDescriptio ...
- windows系统下ftp上传下载和一些常用命令
先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...
- Struts2应用流程注解
当Web容器收到请求(HttpServletReques t)它将请求传递给一个标准的的过滤链包括(ActionContextCleanUp)过滤器. 经过Other filters(SiteMe ...
- CCF I'm Stuck!
问题描述 试题编号: 201312-5 试题名称: I'm stuck! 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', ...
- Centos7 设置Swap分区
1.使用dd命令创建一个swap交换文件 dd if=/dev/zero of=/home/swap bs=1024 count=1024000 2.制作为swap格式文件: mkswap /home ...
- 解决eclipse ctrl+鼠标左键不能用
选择[Window]菜单 Preferences ——>General——>Editors——>Text Editors——>Hyperlinking 把勾都点上,然后确定KE ...
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- SYSIBM.SYSPACKSTMT db2 存储statement的表
SYSIBM.SYSPACKSTMT table The SYSIBM.SYSPACKSTMT table contains one or more rows for each statement ...
- 浅谈 switch和if
1.所有的switch 都可以用if 替换,但所有的if不一定能被switch替换 2.:switch case直接跳到对应的case值里面执行相应代码.而if语句会执行一条一条判断语句,直到匹配到对 ...
- C/C++学习笔记----指针的理解
指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...