Mongodb查找所有子文档都满足条件的文档 [英] Mongodb find a document with all subdocuments satisfying a condition

查看:645
本文介绍了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.

这篇关于Mongodb查找所有子文档都满足条件的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆