Mongsert的Upsert(node.js和mongodb)
问题描述:
我正在学习node.js和mongodb。我在我的应用程序中使用mongoskin模块,但似乎无法获得“upsert”功能。Mongsert的Upsert(node.js和mongodb)
我读过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});