我开始玩'与mongodb有一些问题我仍然无法弄清楚如何解决它们。
给出以下代码(取自book):
var mongodb = require('mongodb');
var server = new mongodb.Server('127.0.0.1', 27017, {});
var client = new mongodb.Db('mydatabase', server, {w: 1});
var tmpID = '';
client.open(function(err){
if(err) throw err;
client.collection('test_insert', function(err, collection){
if(err) throw err;
console.log('we are now able to perform queries.');
collection.insert(
{
"title": "I like cake",
"body": "It is quite good."
},
{safe: true},
function(err, documents){
if(err) throw err;
tmpID = documents.ops[0]._id;
console.log('Document ID is: ' + tmpID);
var _id = new client.bson_serializer.ObjectID(tmpID.toString());
collection.update(
{_id: _id},
{$set: {"title": "I ate too much cake"}},
{safe:true},
function(err){
if (err) throw err;
}
);
}
);
});
});
我希望文件的标题是"我喜欢蛋糕"得到更新。我在这一行得到例外:
var _id = new client.bson_serializer.ObjectID(tmpID);
TypeError: Cannot read property 'ObjectID' of undefined
我检查了client
对象,我看不到bson_serializer ......
nodejs 4.2.1
MondoDB 3.0