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 ...
随机推荐
- Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- C#之显示效果
窗体最大化(包含任务栏): this.TopMost = true; , ); this.Size = Screen.PrimaryScreen.Bounds.Size; 窗体最大化(不包含任务栏): ...
- Appium根据xpath获取控件
如文章< Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通 ...
- 使用Java中的IO流,把A文件里的内容输入到B文件中
我们先创建两个文本文件,out.txt和in.txt,在out.txt中输入"Hello World",然后使用FileInputStream把字符串读取出来,再使用FileOut ...
- effective OC2.0 52阅读笔记(五 内存管理)
第五章:内存管理 29 理解引用计数 30 以ARC简化引用计数 总结:ARC通过命名约定将内存管理规则标准化.其他编程语言很少像OC这样强调命名.ARC通过设置全局数据结构(此数据结构的具体内容因处 ...
- C字符串和指针问题汇总
空指针和传参问题 1) 段错误.形参改为二级指针即可 void GetMemory( char *p ){ p = ( ); } void Test( void ){ char *str = NULL ...
- SpringMVC java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name
当跳转到一个含有form表单的页面的时候 如<form:form commandName="useCarInfo" 必须要new一个useCarInfo的同名实例给jsp来接 ...
- OKhttp的封装(下)
OKhttpManager2.Class 请求工具类 package com.example.administrator.okhttp3; import android.os.Handler; im ...
- JavaScript for循环里边异步操作问题。
问题:(DRIVING.search是异步操作) for循环中做异步操作会导致aDistances数组里边的数据全部都是从A_SHOP_INFO数组中最后一条数据获取的值. var iIdx = 0; ...
- August 8th 2016, Week 33rd Monday
Everything is going on, but don't give up trying. 万事随缘,但不要放弃努力. Every time when I want to give up, y ...