Details
-
Task
-
Resolution: Done
-
Minor - P4
-
None
-
2.8.0
-
None
-
Windows
Description
I would like to use findAndModify to get one document, without doing any modification on it. My need is to find a single document in a collection, or create it if it doesn't exist.
Here's my java code (based on http://api.mongodb.org/java/2.8.0/) and the error I get.
|
java |
MyDocument doc = (MyDocument) myCollection.findAndModify(query, null, null, false, null, true, true);
|
|
|
com.mongodb.CommandResult$CommandFailure: command failed [findandmodify]: { "serverUsed" : "serverDns/ip:port" , "errmsg" : "need remove or update" , "ok" : 0.0}
|
Now let's take a look at the parameters :
query - query to match
fields - fields to be returned
sort - sort to apply before picking first document
remove - if true, document found will be removed
update - update to apply
returnNew - if true, the updated document is returned, otherwise the old document is returned (or it would be lost forever)
upsert - do upsert (insert if document not present)
I have 2 servers that might access this database, so that's why I need an atomic-like "getOrCreate" behavior. There might be a better way of doing this.