开发者

MongoDB-Native NodeJS Update Fails

I have a MongoDB 2.0 install along with NodeJS 0.4.11 and I'm trying to do upserts into a database. Here's the code:

var mongo = require('mongodb');
var 开发者_开发百科db = new mongo.Db('db', new mongo.Server('localhost', 27017, {}), {});

var updatecmd = JSON.parse(JSON.stringify("{ id : " + jsonObj.VisitorID + "}"));
console.log(updatecmd);
var insertObject = JSON.parse(JSON.stringify(temp));
col.update(updatecmd, insertObject, {upsert:true}, function(err, r){console.log(err.stack); console.log(r);});

col.save() works just fine, but when I change it to col.update, I get the following error:

TypeError: Object.keys called on non-object
    at Function.keys (native)
    at Function.calculateObjectSize (/home/admin/node_modules/mongodb/lib/mongodb/bson/bson.js:76:34)
    at [object Object].toBinary (/home/admin/node_modules/mongodb/lib/mongodb/commands/update_command.js:43:112)
    at [object Object].send (/home/admin/node_modules/mongodb/lib/mongodb/connection.js:257:32)
    at [object Object].executeCommand (/home/admin/node_modules/mongodb/lib/mongodb/db.js:746:18)
    at Collection.update (/home/admin/node_modules/mongodb/lib/mongodb/collection.js:421:26)
    at addtoobject (/home/admin/mongoscript.js:127:9)
    at /home/admin/mongoscript.js:103:4
    at EventEmitter.<anonymous> (/home/admin/node_modules/lazy/lazy.js:62:13)
    at EventEmitter.<anonymous> (/home/admin/node_modules/lazy/lazy.js:46:19)

I get that error for every single piece of content. When I do

col.save(insertObject, function(err,r){});

It works just fine.


Not 100% sure if this is the issue, although it looks likely:

var updatecmd = JSON.parse(JSON.stringify("{ id : " + jsonObj.VisitorID + "}"));

This will output a string, not an object. Drop the call to stringify, or better still just build the object.

E.g.:

var ID = 123,
    oldupdatecmd = JSON.parse(JSON.stringify("{ id : " + ID + "}")), // "{id : 123}"
    newupdatecmd = { id : ID }; // {id : 123}   <--  An Object, not a string

Pretty sure the first parameter is supposed to be an object, and that's what the error looks like: Object.keys called on non-object

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜