Mongsert的Upsert(node.js和mongodb)

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

我读过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); 

我如何创build“更新或插入,如果不存在”function?

我还没有尝试过,但根据这里的文档: 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});