gpt4 book ai didi

javascript - 在文档中设置一个字段 - mongoDB

转载 作者:行者123 更新时间:2023-11-30 17:01:13 25 4
gpt4 key购买 nike

我试图在 mongoDB 的文档中设置一个字段,但是当我运行 node app.js 时出现以下错误

throw 者;//未处理的“错误”事件

这是我的代码:

app.get('/verify', function(req, res) {
console.log(req.protocol + ":/" + req.get('host'));
var collection = "usuarios";
collectionDriver.getCollection(collection, function(error, the_collection) {
if (error) {
res.send(400, error);
} else
the_collection.update("{_id:" + req.query.id + "}, {$set: {ativo:'T'}}");
});
});

编辑:

    app.get('/verify',function(req,res){
console.log(req.protocol+":/"+req.get('host'));
var collection = "usuarios";
collectionDriver.getCollection(collection, function(error, the_collection) {
if (error) { res.send(400, error);}
else
the_collection.update({ '_id': req.query.id, $set: { ativo: 'T' } });
// the_collection.update("{_id:'"+req.query.id+"'}, {$set: {ativo:'T'}}");}
});


});

错误

events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (/home/ladessa/files/MelhoraCidade/server/app.js:206:24)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

最佳答案

根据 mongoose's update documentation , update 方法应该用至少两个参数调用。引用函数签名,

Model.update(conditions, update, options, callback);
  1. 条件 - 一个有效的 JavaScript 对象,用于查找要更新的实际文档。

  2. update - 实际的更新对象,这决定了文档中所做的实际更改。

注意 如果您使用的是原生 mongodb 驱动程序,update 函数接受类似的参数。引用 update文档,

update(selector, document, options, callback)

...

selector The selector for the update operation.

document The update document.

所以,你的函数必须这样改

the_collection.update({
'_id': req.query.id
}, {
$set: {
ativo: 'T'
}
});

关于javascript - 在文档中设置一个字段 - mongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28823428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com