说之前我已经将一个文档插入到一个 mongo 集合中。
MongoClient.connect(url, function(err,db){
if(err) {throw err;}
else {
document = {action: "alert",
protocol: "udp",
port: "80",
_id: "12" }
var collection = db.collection("connections");
collection.insertOne(document, function(err,result){
if (err) {throw err;}
else {
console.log("Successful")
db.close();
}
}
}
现在我想更新协议字段。到目前为止我没有运气的是
MongoClient.connect(url, function(err,db){
if (err) { throw err; }
else {
var collection = db.collection("connections");
collection.findOneAndUpdate({_id: "12"}, {$set: {protocol: "http"}}, {new: true}, function(err,doc) {
if (err) { throw err; }
else { console.log("Updated"); }
});
}
});
我是否将错误的参数传递给 findOneAndUpdate 方法?我正确连接到数据库。
原文由 Spothedog1 发布,翻译遵循 CC BY-SA 4.0 许可协议
我觉得你应该试试
如果“upsert”设置为 true,则在没有文档匹配查询条件时创建一个新文档。