Frage

I'm trying to update a certain field in my mongodb collection 'users'

Example of a document:

{
  _id: ObjectId("536d4d3307221f847fbbea49"),
  groups: [
    {
      match_id: "dc3acf1b",
      groupName: "World Cup, Group A",
      score:[0,1]
    },
    {
      match_id: "d4f14940",
      groupName: "World Cup, Group A",
      score:[2,1]
    }],
  name:"Foo Bar",
  points:10
}

I'm trying to update only score fields that have a certain score..

here is what I want (position operator) :

db.users.update({'groups.match_id':i_match , 'groups.score':i_score},
                {$inc:{points:1}});

Now I figure this would look for a user that has both those fields, but I want to look in the same groups object that had the first part of the query.

As in:

 db.users.update({'groups.match_id':d4f14940 , 'groups.score':[2,1]},
                    {$inc:{points:1}});

should give the user +1 points

But:

 db.users.update({'groups.match_id':dc3acf1b , 'groups.score':[2,1]},
                        {$inc:{points:1}});

Should not

You get my drift :)

Now I know from the positional operator that I should use the '$' sign, but I think that can only be used in the second paramater of the update() - the updating part..and not the finding part

note - The actual comparison of the score in this way (arrays) might not work..but that's not the issue here

War es hilfreich?

Lösung

What you want is the $elemMatch operator. This allows you to select array elements that require multiple fields to match a condition:

db.users.update(
    { "groups": { 
        "$elemMatch": {
          "match_id": "d4f14940",
          "score": [2,1]
        }      
    }},
    { "$inc": { "points": 1 } }
)

So that allows you to match the document as the "groups" array contains an element that has both match_id and score that meet the conditions you supplied. If you change that match_id without altering the score, then the document does not match and the increment is not made to your points value.

So this has nothing to do with the positional $ operator in this case as you are not updating and array at a matched position, which is what that is for.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top