Adrian A
Adrian A

Reputation: 452

MongoDB push into nested array

I have this scheme

{
    "_id": {
        "$oid": "5e187b1791c51b4b105fcff0"
    },
    "username": "test",
    "email": "[email protected]",
    "role": "admin",
    "userScoreFantasy": {
        "tournaments": [
            {
                "tournament_id": {
                    "$oid": "5e1892fb480f344830a3f160"
                },
                "predictions": [],
                "score": null
            },
            {
                "tournament_id": {
                    "$oid": "5e189f5f8d88292754e10b37"
                },
                "predictions": [],
                "score": null
            }
        ],
        "totalScore": null
    },
}

I want to do this :

  1. Find user with a predefined user id
  2. Pass all userScoreFantasy.tournaments array to find a specific tournament id
  3. Push into the found tournament predictions array an object like this one :
{
    score,
    "match_id": foundMatch._id
}

So the tournaments array will become :

[
    {
        "tournament_id": {
            "$oid": "5e1892fb480f344830a3f160"
        },
        "predictions": [
            "score" : 200,
            "match_id": "5e1892fb480f34faw21410"
        ],
        "score": null
    },
]

I tried to do this :

 Users.update({
                      "_id": prediction.user_id,
                      "userScoreFantasy.tournaments": {
                        "$elemMatch": {
                          "tournament_id": foundMatch.tournament_id
                        }
                      }
                    }, {
                      "$push": {
                        "userScoreFantasy.tournaments.$.predictions": {
                          score,
                          "match_id": foundMatch._id
                        }
                      }
                    })

But the array is not updating.

EDIT : Working call :

Users.updateOne({
                      "_id": ObjectID(prediction.user_id),
                    }, {
                      $addToSet: {
                        "userScoreFantasy.tournaments.$[element].predictions": {
                          score,
                          "match_id": foundMatch._id
                        }
                      }
                    }, {
                      arrayFilters: [{
                        "element.tournament_id": ObjectID(foundMatch.tournament_id)
                      }]
                    }

                  )

Upvotes: 2

Views: 58

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22316

You should use the position indentifier to update your arrays, like so:

Users.updateOne(
    {
        "_id": prediction.user_id,
    },
    {
        $addToSet: {
            "userScoreFantasy.tournaments.$[element].predictions": {
                score,
                "match_id": foundMatch._id
            }
        }
    },
    {arrayFilters: [{"element.tournament_id": foundMatch.tournament_id}]}
);

Upvotes: 2

Related Questions