gpt4 book ai didi

node.js - 使用 Mongoskin(node.js 和 mongodb)进行更新

转载 作者:IT老高 更新时间:2023-10-28 13:34:11 26 4
gpt4 key购买 nike

我正在学习 node.js 和 mongodb。我在我的应用程序中使用 mongoskin 模块,但我似乎无法让“upsert”功能正常工作。

我已经阅读了 github 上的(相当不透明的)mongoskin 指南。到目前为止,这是我尝试过的:

// this works.  there's an insert then an update.  The final "x" is "XX".
db.collection("stuff").insert({a:"A"}, {x:"X"});
db.collection("stuff").update({a:"A"}, {x:"XX"});

// this does NOT work. I thought it would do an upsert, but nothing.
db.collection("stuff").update({b:"B"}, {y:"YY"}, true);

如何创建“如果不存在则更新或插入”功能?

最佳答案

我没有尝试过,但根据这里的文档:https://github.com/guileen/node-mongoskin#inherit-updating在这里:https://github.com/christkv/node-mongodb-native/blob/master/docs/insert.md ,看起来 options 是第三个参数,应该是一个对象,像这样:

db.collection("stuff").update({b:"B"}, {y:"YY"}, {upsert:true});

关于node.js - 使用 Mongoskin(node.js 和 mongodb)进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367363/

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