做一个困难的 mongoDB 查找
Doing a difficult mongoDB find
我需要通过文档中数组中的用户 ID 查找文档,看起来像
{
"uuid": "000-000-000whatever",
users:
[
{
"id":"id1",
"role":"role1"
},
{
"id":"id2",
"role":"role1"
}
]
}
虽然我知道我可以在 mongo 中做“users.id”作为过滤器,但我不知道如何使用 Go 中的 bson 来做。
我能想到的最聪明的事情是
bson.M{"users": bson.A{bson.M{"id":id}}}
但不用说,它没有用。
转到中的“相同”:
bson.M{"users.id": id}
我需要通过文档中数组中的用户 ID 查找文档,看起来像
{
"uuid": "000-000-000whatever",
users:
[
{
"id":"id1",
"role":"role1"
},
{
"id":"id2",
"role":"role1"
}
]
}
虽然我知道我可以在 mongo 中做“users.id”作为过滤器,但我不知道如何使用 Go 中的 bson 来做。
我能想到的最聪明的事情是
bson.M{"users": bson.A{bson.M{"id":id}}}
但不用说,它没有用。
转到中的“相同”:
bson.M{"users.id": id}