Mongodb查找所有子文档都满足条件的文档

问题描述:

我的数据库中有游戏收藏:

I have Game collection in my DB:

var game = {
  players: [{username:"user1", status:"played"},
            {username:"user2", status:"accepted"}]
}

据我了解的查询是这样的:db.games.find({"players.status":"played"})将为我提供至少一名玩家处于玩过"状态的所有游戏.如何找到所有状态均为已玩过"的玩家的游戏?

As far as I understand query like this: db.games.find({"players.status":"played"}) will give me all games where at least one player has status "played". How can I find games with ALL players having status "played"?

如果除已播放"状态外,您只有其他一种状态,请使用查询:

If you only have one other status than "played" use the query:

db.games.find({ "players.status": { $ne:"accepted" } })

您可以调整查询以处理更多状态值,只要在查询时就知道它们即可.

You can adjust the query to handle more status values, as long as they are all known at the time of the query.