I would like to create a counter that starts at 1000.
I.e. upsert a mongodb document as follows
- if the document does not exist, create it with value 1000
- if the document already exists, increment the value +1
The following does not work:
environment_data:PRIMARY> db.test.findAndModify
({query:{key:"mycounter"},
update:{$inc:{value:1}, $set:{value:1000}},
upsert:true})
Error: findAndModifyFailed failed: {
"errmsg" : "exception: Cannot update 'value' and 'value' at the same time",
"code" : 16836,
"ok" : 0
}
Is there a way to tell the upsert that for an insert, it needs to $set 1000, but for an update, it needs to $inc 1 ?