OK, I am new to MongoDB, but have learned quite a bit, I understand all the $ operations and how I can use them on the basic side of things. But when things get a little more complex, it just confuses the heck out of me how to get something to work.So right now, our code will find the entire document, then in Groovy we change the document, then save the entire document back into Mongo as it is much easier to understand and took ten minutes to code.But we really want to do it the right way and do updates to the document, so that we can also get an atomic operation on the change. even change the WriteConcern to make it transactional.But the problem is the find portion of the update statement is complex as well as the update section ends up being complex.Out document is a game table document with an array of players. Each table can have a finite number of players at the table max, but each table might have a different max. Inside the array of players is also the seat that that player is in. So when someone comes along and wants to sit in a seat, we need to do an update that pushes that player into the players array, as long as no one is sitting in that seat.Here is an example document{ "_id" : "09971b6e-5745-4f29-aa59-67e6d5a7d754","gameConfig" : { "speed" : "normal", "seats" : "5" },"name" : "Risk","playerCount" : 2,"players" : [{"id" : "777","name" : "Bugs Bunny","seat" : 2},{"id" : "776","name" : "Elmer Fudd","seat" : 3,}]}So now I have a new player that wants to sit at the table. In my update I first need to make sure there is room at the table. Currently the tables is configured to have at most 5 players, and there are only 2 at the table, so that would pass. It would also have to check to make sure that the seat that the new player is requesting is available. So neither Bugs nor Elmer can be sitting in it.So if the new player wants seat 2 or 3, the update fails, but 1, 4, or 5 and they are good.Here are some update statements I tried, but didn't work. mostly syntax errors.db.tables.update({"_id":"09971b6e-5745-4f29-aa59-67e6d5a7d754", "players.$.seat":{$ne:3}}, update like below)db.tables.update({"_id" : "09971b6e-5745-4f29-aa59-67e6d5a7d754",
"playerCount" : {$lt: gameConfig.seats}, "players.$.seat": {$nin: seatAskedFor}, {$push : {players : player}, $inc : {playerCount : 1}}}
The last one seems to me very close to what I want, but not sure how to do the $nin for looking at each seat assigned already. Also, how the $lt will work if I need to compare two property values within a document, instead of me supplying the exact value like I do with id.Thanks for you help.Mark