我在通过Node + Express和Mongodb通过Mongoskin更新文档时遇到了问题。我通过mongo命令行使用了以下内容,并按预期工作:
db.userlist.update({_id: ObjectId('5377821219f21e974150bacf')}, {$set: {username: "Test"}})但是,在Node中执行类似的行不起作用,并且似乎通过浏览器返回了一个500错误。
db.collection('userlist').update({_id: ObjectId('5377821219f21e974150bacf')}, {$set: {username: "Test"}});我尝试过传递多个选项: true和false,也尝试添加回调,但每次都不起作用。
我遗漏了什么?附注:我对node和mongodb非常陌生,谢谢。
发布于 2014-05-17 18:02:41
您的语法已经关闭-- mongoskin的语法与通过mongoskin在node.js中调用函数的语法不同。我已经根据本教程更新了您的代码,使其能够工作:http://www.hacksparrow.com/mongoskin-tutorial-with-examples.html:
var mongo = require('mongoskin');
require('mongodb');
var db = mongo.db("mongodb://localhost:27017/mongoskin", {native_parser:true});
db.collection('userlist').update({_id: mongo.helper.toObjectID("5377821219f21e974150bacf")}, {'$set':{username:"Test"}}, function(err, result) {
if (err) throw err;
if (result) console.log('Updated!');
});注意mongo.helper.toObjectID()。来自文档https://github.com/kissjs/node-mongoskin:
collection.update({_id: toObjectID(id)}, ...)https://stackoverflow.com/questions/23713565
复制相似问题