简体   繁体   中英

Add new element to mongoDB document array only if a certain condition is met

I have this mongoDB document structure:

Game = {
  _id: 'randomObjectId',
  players: [Array of players ObjectIds],
  maxPlayers: 2,
  status: 'created' (or 'fully-booked')
}

When a new player joins the game I want to add him to the players array and change the status of the game to fully-booked only if this condition is met: players.length < maxPlayers .

To recap:

  • Check inside the DB(using the query) if players.length < maxPlayers ;
  • If yes update the document
  • If no send back an error response (optional)

Without this check it's easy to make the query using:

 Game.findByIdAndUpdate('randomObjectId', { $addToSet: { players: 'playerObjId' }, gameStatus: 'fully-booked' }, { new: true })

What I don't know is how to add this condition to this (or other type of) query. Any help is appreciated. Thanks

you can try an aggregation pipeline update like the following. however, if already fully booked, the command will return a null . also note that if you try to add a player id that already exists in the array, it won't be added.

db.collection.findOneAndUpdate(
    {
        _id: someObjectId,
        $expr: {
            $lt: [{ $size: "$players" }, "$maxPlayers"]
        }
    },
    [
        {
            $set: {
                players: { $setUnion: ["$players", [newPlayerObjectId]] }
            }
        },
        {
            $set: {
                status: {
                    $cond: {
                        if: { $lt: [{ $size: "$players" }, "$maxPlayers"] },
                        then: "created",
                        else: "fully-booked"
                    }
                }
            }
        }
    ],
    {
        returnNewDocument: true
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM