Sir Robert
Sir Robert

Reputation: 4976

Upsert with Mongoskin (node.js and mongodb)

I'm learning node.js and mongodb. I'm using the mongoskin module in my app, but I can't seem to get "upsert" functionality to work.

I've read the (rather opaque) mongoskin guide on github. Here's what I've tried so far:

// 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);

How can I create "update or insert if not exists" functionality?

Upvotes: 5

Views: 3665

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33175

I've not tried it, but according to the doc here: https://github.com/guileen/node-mongoskin#inherit-updating and here: https://github.com/christkv/node-mongodb-native/blob/master/docs/insert.md, it looks like options is the third parameter, and it's supposed to be an object, like so:

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

Upvotes: 9

Related Questions