I have the following (simplified) data:
db={
Rankings: [
{
_id: ObjectId("62c66dc612296752b7c82cde"),
players: [
{
playerId: "xyz1",
challengerId: "abc1",
rank: 1
},
{
playerId: "abc1",
challengerId: "xyz1",
rank: 2
}
],
}
]
}
and I want to do a bulkWrite in the same document against rank 1 and 2 with expected output:
[
{
"_id": ObjectId("62c66dc612296752b7c82cde"),
"players": [
{
"challengerId": null,
"playerId": "abc1",
"rank": 1
},
{
"challengerId": null,
"playerId": "xyz1",
"rank": 2
}
]
}
]
For reference/context I believe my closest attempt so far is (simplified ids are now full ObjectIds, but the same swapping logic applies):
db.Rankings.bulkWrite([
{ updateOne : {
//here the challenger is 62c2b79d966b72973fe52317, but he has won, so he
//becomes 'player' in rank 1
"filter" : { _id: ObjectId("62c66dc612296752b7c82cde"),
input: "$players",
as: "players",
cond: { $eq: [ "$$players.ranking", 1 ] }},
"update" : { $set : { "players.playerId": ObjectId("62c2b79d966b72973fe52317"),
"players.challengerId": null
} }
} }
,
{ updateOne : {
//player from rank 1, is now player in rank 2
"filter" : { _id: ObjectId("62c66dc612296752b7c82cde"),
input: "$players",
as: "players",
cond: { $eq: [ "$$players.ranking", 2 ] }},
"update" : { $set : { "players.playerId": ObjectId("62c2b79d966b72973fe52316"),
"players.challengerId": null
} }
} }
])
This returns:
{
"acknowledged": true,
"insertedCount": 0,
"insertedIds": {},
"matchedCount": 0,
"modifiedCount": 0,
"deletedCount": 0,
"upsertedCount": 0,
"upsertedIds": {}
}
What should my “filter” syntax be to filter against rank in the players object array and achieve these updates? thanks …