Re: Trying to update with complex finder and I am confused

20 views
Skip to first unread message

Bytor99999

unread,
Oct 24, 2012, 11:13:22 PM10/24/12
to mongod...@googlegroups.com
After many hours of research, I found that I cannot use a value within a document inside the criteria portion of an update on the right side. So nothing like {seat: {$nin: players.seat}} where players.seat would be from the queryed collection document, and not from some outside source. 

Or as a better example
{$lt: gameConfig.seats}

Is not legal in update. Because gameConfig.seats is part of the document not some value I am passing in to the query.

So, I decided to test out the approach where all the seats are in the array, whether a player is assigned to it or not. So null values. If a table has 10 seats, I have 10 items in the player array with player information null, but seat number assigned. Then my update will look like


db.tables.update({_id: jointTableRequest.tableId,
      "players.seat" : joinTableRequest.seat,
      "players.name" : { $empty: true},
      newObj: {$set : {"players.id" : joinTableRequest.id,
               "players.name" : joinTableRequest.name,
               "players.username" : joinTableRequest.username,
               "players.cash" : joinTableRequest.cash,
               "players.sitting": true},
       $inc : {playerCount : 1}
    })

I think that will work. At least it makes sense to me.

Thanks

Mark

On Tuesday, October 23, 2012 8:24:12 PM UTC-7, Bytor99999 wrote:
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

Reply all
Reply to author
Forward
0 new messages