MGO 查询对象的嵌套数组
MGO Query nested array of objects
我
我很难将 MongoDB 查询转换为 mgo bson。 Mongo 记录模式如下所示。我想查找主题标签为 "Education" 和 "Students".
的记录
db.questions.insert
(
{
"_id" : ObjectId("5cb4048478163fa3c9726fdf"),
"questionText" : "why?",
"createdOn" : new Date(),
"createdBy": user1,
"topics" : [
{
"label": "Education",
},
{
"label": "Life and Living",
},
{
"label": "Students"
}
]
}
)
使用 Robo 3T,查询如下所示:
db.questions.find({$and : [
{"topics": {"label": "Students"}},
{"topics": {"label": "Education"}}
]})
我在使用 MGO 建模时遇到问题。目前,已经试过这个:
map[$and:[
map[topics:map[label:students]]
map[topics:map[label:life and living]]
]]
还有这个
map[topics:map[$and:[
map[label:students]
map[label:life and living]
]]]
如果你想从嵌套数组中找到一些值,那么你可以使用 $elemMatch 方法。
db.questions.find(
{$and:
[
{topics: {$elemMatch: {label: 'Students'}}},
{topics: {$elemMatch: {label: 'Education'}}}
]
}
)
上述答案的bson模型如下:
query = getAndFilters(
bson.M{"topics": bson.M{"$elemMatch": bson.M{"label": "Students"}}},
bson.M{"topics": bson.M{"$elemMatch": bson.M{"label": "Education"}}})
我 我很难将 MongoDB 查询转换为 mgo bson。 Mongo 记录模式如下所示。我想查找主题标签为 "Education" 和 "Students".
的记录db.questions.insert
(
{
"_id" : ObjectId("5cb4048478163fa3c9726fdf"),
"questionText" : "why?",
"createdOn" : new Date(),
"createdBy": user1,
"topics" : [
{
"label": "Education",
},
{
"label": "Life and Living",
},
{
"label": "Students"
}
]
}
)
使用 Robo 3T,查询如下所示:
db.questions.find({$and : [
{"topics": {"label": "Students"}},
{"topics": {"label": "Education"}}
]})
我在使用 MGO 建模时遇到问题。目前,已经试过这个:
map[$and:[
map[topics:map[label:students]]
map[topics:map[label:life and living]]
]]
还有这个
map[topics:map[$and:[
map[label:students]
map[label:life and living]
]]]
如果你想从嵌套数组中找到一些值,那么你可以使用 $elemMatch 方法。
db.questions.find(
{$and:
[
{topics: {$elemMatch: {label: 'Students'}}},
{topics: {$elemMatch: {label: 'Education'}}}
]
}
)
上述答案的bson模型如下:
query = getAndFilters(
bson.M{"topics": bson.M{"$elemMatch": bson.M{"label": "Students"}}},
bson.M{"topics": bson.M{"$elemMatch": bson.M{"label": "Education"}}})