2

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)
                      }]
                    }

                  )

1 Answer 1

2

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}]}
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It works, i did some edits to your answer putting ObjectID for _id and everything turned fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.