Hi
I have my structure like this
[
{
"_id": 12175,
"MatchID": 11978,
"Players": [
{
"PlayerID": 12063,
"PlayerPosition": "Captain",
"Points": 8
},
{
"PlayerID": 12041,
"PlayerPosition": "Player",
"Points": 3
},
{
"PlayerID": 12066,
"PlayerPosition": "Player",
"Points": 21
},
{
"PlayerID": 12067,
"PlayerPosition": "Player",
"Points": 33
},
{
"PlayerID": 12064,
"PlayerPosition": "Player",
"Points": 0
},
{
"PlayerID": 12069,
"PlayerPosition": "ViceCaptain",
"Points": 12288
},
{
"PlayerID": 12045,
"PlayerPosition": "Player",
"Points": 0
},
{
"PlayerID": 12074,
"PlayerPosition": "Player",
"Points": -3
},
{
"PlayerID": 12079,
"PlayerPosition": "Player",
"Points": 8
},
{
"PlayerID": 12059,
"PlayerPosition": "Player",
"Points": 0
},
{
"PlayerID": 12054,
"PlayerPosition": "Player",
"Points": 0
}
],
"Points": 3141
}
]
Now I am trying to update the player’s point using the below query
db.user_teams.updateMany(
{"MatchID": 11978, "Players.PlayerID": 12063},
{
$set: { 'Players.$.Points' : { $switch: {
branches: [
{ case: { $eq: [ "$Players.$.PlayerPosition", "Captain" ] }, then: 16 },
{ case: { $eq: [ "$Players.$.PlayerPosition", "ViceCaptain" ] }, then: 12 }
],
default: 8
} } }
}
);
But this is giving me error
com.mongodb.MongoWriteException: The dollar ($) prefixed field ‘$switch’ in ‘Players.0.Points.$switch’ is not valid for storage.
I also tried with
db.user_teams.updateMany(
{"MatchID": 11978, "Players.PlayerID": 12063},
[
{
$set: { 'Players.$.Points' : { $switch: {
branches: [
{ case: { $eq: [ "$Players.$.PlayerPosition", "Captain" ] }, then: 16 },
{ case: { $eq: [ "$Players.$.PlayerPosition", "ViceCaptain" ] }, then: 12 }
],
default: 8
} } }
}
]
);
Getting error
com.mongodb.MongoWriteException: Invalid set :: caused by :: FieldPath field names may not start with ''.
Please help me to resolve this error.