nodejs系列之七——nodejs与mongoDB
原文: http://ramonblog.cloudfoundry.com/blog/4fd4536715d8f0d91e000002
参考其它内容请看本blog中的nodejs系列提纲
更多关于MongoDB的NodeJS驱动内容请参考MongoDB NodeJS Driver
简介
No-SQL的一篇比较详细的中文介绍来自于我自然的博客。而MongoDB也是NoSQL中的一个实现。只不过它现在比较流行于Web开发,尤其是跟NodeJS的结合。
开始1. 安装mongodb on Mac安装包管理软件brew
访问HomeBrew可以轻易地安装brew,它可以很轻松地为你在Mac上安装Unix的程序,譬如mongodb在linux上很容易安装,但是并没有Mac原始的安装包,由于Mac本身也是基于Unix的,所以brew究提供了类似于Linux的简单安装方式。安装mongodb
首先更新brew
brew update
然后安装mongodb:
brew install mongodb
安装成功之后,会自动把mongodb/bin加入到PATH底下,也就是说你可以直接在命令行下运行mongodb相关的命令。启动mongodb如下:
mongod
注意:Linux或Windows的安装可以参考官方文档:Installing MongoDB
2. 命令行访问mongodb
安装并运行mongodb成功之后,可以运行下面的命令连接mongo db:
localhost:myblog ramon$ mongoMongoDB shell version: 2.0.3connecting to: test
- 安装包管理软件brew
访问HomeBrew可以轻易地安装brew,它可以很轻松地为你在Mac上安装Unix的程序,譬如mongodb在linux上很容易安装,但是并没有Mac原始的安装包,由于Mac本身也是基于Unix的,所以brew究提供了类似于Linux的简单安装方式。
安装mongodb
首先更新brew
brew update
然后安装mongodb:
brew install mongodb
安装成功之后,会自动把mongodb/bin加入到PATH底下,也就是说你可以直接在命令行下运行mongodb相关的命令。启动mongodb如下:
mongod
注意:Linux或Windows的安装可以参考官方文档:Installing MongoDB
2. 命令行访问mongodb
安装并运行mongodb成功之后,可以运行下面的命令连接mongo db:
localhost:myblog ramon$ mongoMongoDB shell version: 2.0.3connecting to: test类似与mysql,运行show dbs可以查看当前server有几个数据库,例如本机的运行情况:
> show dbsdb 0.203125GBlocal (empty)node-mongo-blog 0.203125GBtest 0.203125GB可以看出,这里有4个数据库。你也可以运行命令help以查看帮助。现在创建数据库foo,
> use fooswitched to db foo输入命令db可以查看当前数据库的名字。而要查看当前数据库有哪些集合(Collection),也就是关系数据库中的表可以用如下的命令
> show collections显示没有任何集合,现在创建collection bar:
> db.bar.insert({a: 1, b: 'b1'})> show collectionsbarsystem.indexes对于collection的操作,必须要在collection前面加上db.才行,否则报错。只要尝试往bar插入数据,那么bar就会被创建,根本不需要事先定义bar,而bar的schema也是任意的,也就是说现在插入的数据是{a:1, b: "b1"},接下来你也可以插入{c: 2, b: "b2"}.至于在查看集合的时候,发现有system.indexes,它是系统生成的,用于存储所有的索引(Index)信息,参考Mongo Metadata。
> db.bar.insert({c:2, b:'b2'})> db.bar.find(){ "_id" : ObjectId("4fde10be4245dec7bc76e27e"), "a" : 1, "b" : "b1" }{ "_id" : ObjectId("4fde118f4245dec7bc76e27f"), "c" : 2, "b" : "b2" }我们用find命令来查看collection的数据。你可以看到自动增加了字段_id为主键。
更多的关于mongodb的入门介绍可以参考官方的Tutorial
3. 安装mongodb nodejs driver
要安装mongodb nodejs的原生driver,可以通过npm,也可以通过github下载:
npm install mongodb
连接到数据库var mongo = require('mongodb'), Server = mongo.Server, Db = mongo.Db;var server = new Server('localhost', 27017, {auto_reconnect: true});var db = new Db('foo', server);db.open(function(err, db) { if(!err) { console.log("We are connected"); }});
查询Getdb.open(function(err, db) { if(!err) { console.log("We are connected"); db.collection('bar', function(err, collection){ collection.find().toArray(function(error, bars){console.log(bars);}); collection.find({a:1}).toArray(function(error, bars){console.log(bars);}); collection.findOne({a: 1}, function(error, bar){console.log(bar)}); }); }});
var mongo = require('mongodb'), Server = mongo.Server, Db = mongo.Db;var server = new Server('localhost', 27017, {auto_reconnect: true});var db = new Db('foo', server);db.open(function(err, db) { if(!err) { console.log("We are connected"); }});db.open(function(err, db) { if(!err) { console.log("We are connected"); db.collection('bar', function(err, collection){ collection.find().toArray(function(error, bars){console.log(bars);}); collection.find({a:1}).toArray(function(error, bars){console.log(bars);}); collection.findOne({a: 1}, function(error, bar){console.log(bar)}); }); }});这里一旦用find获取到结果集之后,需要调用toArray方法,并传递回调函数,这个时候才能拿到真正的数据。第二个find语句则是查找所有a=1的文档。第三个find语句则是只找第一个满足条件的文档。具体更多的过滤条件可以参考官方的Query语句。这里有个有趣的事情需要注意,find本身并不执行查询,它只是返回一个Cursor实例,你可以遍历这个Cursor来查询数据。如下面的例子:
// Cursors don't run their queries until you actually attempt to retrieve data // from them. // Find returns a Cursor, which is Enumerable. You can iterate: collection.find(function(err, cursor) { cursor.each(function(err, item) { if(item != null) console.dir(item); }); }); // You can turn it into an array collection.find(function(err, cursor) { cursor.toArray(function(err, items) { console.log("count: " + items.length); }); });插入Insertdb.open(function(err, db) { if(!err) { db.collection('bar', function(err, collection) { var doc1 = {a: 1}; var doc2 = {a: 2, b: 'b2'}; var docs = [{a:3}, {a:4}]; collection.insert(doc1); collection.insert(doc2, {safe:true}, function(err, result) {}); collection.insert(docs, {safe:true}, function(err, result) {}); }); }});
db.open(function(err, db) { if(!err) { db.collection('bar', function(err, collection) { var doc1 = {a: 1}; var doc2 = {a: 2, b: 'b2'}; var docs = [{a:3}, {a:4}]; collection.insert(doc1); collection.insert(doc2, {safe:true}, function(err, result) {}); collection.insert(docs, {safe:true}, function(err, result) {}); }); }});第一个insert和第二个insert的区别在于,增加了一个option对象参数({safe:true})以及一个回调函数。MongoDB的insert/update/remove都是异步的,也就是说发出insert命令之后,就不管数据库是否执行成功了。要想知道数据库是否执行成功,需要再发出一个查询请求来获取连接(Connection)的最后一个错误状态。为了简化这个过程,也就支持{safe:true}这个参数,使得insert和错误状态查询能够一起执行,一旦设置这个参数,一定要增加回调函数作为第三个参数。具体地,我们可以看下面地例子来理解这个{safe:true}的意义:
db.collection('bar', function(err, collection){ collection.insert({a:996, _id:'1'}, function(error, bars){ console.log('insert success without safe'); console.log(error); console.log(bars); collection.insert({a:996, _id:'1'}, {safe:true}, function(error, bars){ console.log('insert fail with safe and get error'); console.log(error); console.log(bars); collection.insert({a:996, _id:'1'}, function(error, bars){ console.log('insert fail without safe but no error'); console.log(error); console.log(bars); }); }); });});# output result[app.js] insert success without safe[app.js] null[app.js] [ { a: 996, _id: '1' } ][app.js] insert fail with safe and get error[app.js] { [MongoError: E11000 duplicate key error index: foo.bar.$_id_ dup key: { : "1" }] name: 'MongoError', err: 'E11000 duplicate key error index: foo.bar.$_id_ dup key: { : "1" }', code: 11000, n: 0, connectionId: 38, ok: 1 }[app.js] undefined[app.js] insert fail without safe but no error[app.js] null[app.js] [ { a: 996, _id: '1' } ]这里的_id是mongodb默认的主键,是不允许重复的。如果你传入了_id则以传入的值作为主键,如果没有传入则会自动生成。你可以看到,第一次insert,我们也不关心是不是真的插入了,幸运的是真的成功了,因为不存在_id为1的数据。第二次插入的时候,我们设置{safe:true}以确保一定插入成功,这是会报主键重复的错误。第三次同样的插入,但是不设置{safe:true},这个时候发现并没有报错,而且回调函数还拿到了要插入的数据。是不是第三次插入成功了呢?不是的,其实正像第二次插入的一样,肯定是主键重复了,但是由于我们并没有要求返回最后的错误状态,所以mongodb drvier直接回调了我们传入的回调函数,并且设置error为null,bars为要插入的数据。总结一下,如果你要确保数据是否更新(insert/update/remove)成功必须要设置{safe:true}选项。
更新Updatecollection.update({a:996}, {$push: {b:'b'}}, function(error, bars){});collection.update({a:996}, {$set: {a:997}}, function(error, bars){});
collection.update({a:996}, {$push: {b:'b'}}, function(error, bars){});collection.update({a:996}, {$set: {a:997}}, function(error, bars){});注意,这里为了代码简单,我们没有设置{safe:true}。你可以看到update的第一个参数是条件,即对a=996的文档进行更新。第二个参数则是表示要如何更新文档,譬如第一个update是增加一个属性b,且设置其值为字符串'b';第二个update是修改a的值为997。可以看出,第二个参数是一个对象,其属性名是一个操作符,以$开头,值为一个对象。这些操作符除了这里的$push和$set,还有其它的$inc, $unset, $pushAll等等,具体可以参考这里。
删除Deletecollection.remove({a:997}, {safe:true}, function(error, count){ console.log(error); console.log(count); collection.remove();});
collection.remove({a:997}, {safe:true}, function(error, count){ console.log(error); console.log(count); collection.remove();});这里第一个remove是删除a=997的所有文档。回调函数的第二个参数是表示相应删除的文档数量。第二个remove则是删除该collection中的所有文档。
高级1. sort
collection.find({}, {sort: [['created_at', 'desc'], ['body', 'asc']]})
其中'desc'也可以用-1表示,而'asc'可以用1表示。如果是只有一个sort列,也可以用下面的方式
collection.find({}, {sort: {'created_at': -1}})
注意:这里用一个对象表示sort的时候,排序方向必须是1(升序)或者-1(降序)。可以说这是一个很垃圾的API设计。首先不应该用数组的数组来表示sort;而只用一列排序时只能用数字不能用字符串更加是API的不一致
2. limit
collection.find({}, {limit: 10, skip:20})
这个可以用来做分页,表示获取从第20条(第1条记录序号为0)记录开始的10条记录.类似与Mysql的limit 20, 10.
3. count
collection.count({}, function(err, count){...} )
第一个参数是query对象,可以省略。第二个参数是callback函数。