mongodb - Check first value in array, insert new conditionally -


I have an array of "states" in my documents:

  { "_id : ObjectId ( "53026de61e30e2525d000004") "states": [{ "name": "full", "user id": ObjectId ( "52f4576126cd0cbe2f000005"), "_ id": ObjectId ( "53026e16c054fc575d000004")}, { "name" the "active", "user id": ObjectId ( "52f4576126cd0cbe2f000005"), "_ id": ObjectId ( "53026de61e30e2525d000004")}]}  

I'm just a new state array when On the front is a new state. Currently the work is going on until Mongo 2.6 has been released here:

However I do not want users to save the same line twice. If IE is already complete, then you should not be able to add another 'full' state. Is there any way that I see the first element in the array can I can I insert only new state if it is not the same as Mongo in a query / update command.

I say that a query / update du e fact that hours of Mongolian transactions Not does not so I do not want to query the first element in the array, and then sends another update statement said, because no other problem with my query and went to put between my updates.

After

you qualify for your update statement :

  db.mydb.states.update ({ "states.name": {$ Nin: [ "newstate"]}}, {$ addToSet: { "states": { "name": "newstate    to update any of  also does not return the document  

this will prevent the update from a user. you Additional code can be added to filter on the query part.


Comments