2012-04-28 66 views
5

我正在学习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); 

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

回答