使用Mongoskin进行Upsert(node.js和mongodb)

Sir*_*ert 5 upsert mongodb node.js

我正在学习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);
Run Code Online (Sandbox Code Playgroud)

如何创建"更新或插入(如果不存在)"功能?

Eve*_*man 9

我没试过,但根据这里的文档: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});
Run Code Online (Sandbox Code Playgroud)