我有一个结构如下的 MongoDB 文档:
{
"user_id": 7387,
"first_name": "Daniel",
"roles": [
{
"role_id": "509c20144fd846549e8d42ef",
"role_name": "Writer",
"is_lead": 0
},
{
"role_id": "509c20344fd846549e8d42f0",
"role_name": "Editor",
"is_lead": 1
}
]
},
{
"user_id": 15735,
"first_name": "Tom",
"roles": [
{
"role_id": "509c203f4fd846549e8d42f1",
"role_name": "Admin",
"is_lead": 0
},
{
"role_id": "509c20144fd846549e8d42ef",
"role_name": "Writer",
"is_lead": 1
},
{
"role_id": "509c20344fd846549e8d42f0",
"role_name": "Editor",
"is_lead": 0
}
]
}
如何找到所有将 roles.role_name 作为 Writer 并且将 roles.is_lead 作为 1 的用户(所有作家也是潜在客户)?
我试过:
db.users.find({$and:[{"roles.role_name":"Writer","roles.is_lead":1}]});
在上面的例子中,它应该只返回“Tom”。但这会返回“Tom”和“Daniel”(我猜它选择 {roles.is_lead:1} 作为“Daniel”的编辑角色。我该如何解决这个查询?
谢谢。