I have a game collection that looks like this:
[
{
_id: ObjectId("6314dc4de4ad4c8141ce0b08"),
status: "started",
channel: "myChannel",
teams: [
{
name: "myFirstTeam",
score: 0,
users: [
{
id: 9082376,
name: "myFirstUser"
},
{
id: 289168,
name: "mySecondUser"
},
]
},
{
name: "mySecondTeam",
score: 0,
users: [
{
id: 898323,
name: "myThirdUser"
}
]
}
]
}
]
I managed to add a user to a team of a specific size:
db.collection.update({
"channel": "myChannel",
"teams.users": {
$size: 1
}
},
{
$push: {
"teams.$.users": {
id: 23424234,
name: "myUserName"
}
}
})
My goal is to add a user to a specific game on the smallest team. I'm new with mongodb, I don't even know if that's possible with a request only. I see the $min and $count but I can't find how to use it.
You can try on this playground
BONUS: Check to make sure the userId added is not already on any team of this game, inside the query (but I can check that before or/and after)