我收集的數據是這樣的:在貓鼬返回內部數據
{
"_id" : ObjectId("590ad1f747627f86e585a4af"),
"code" : "IR",
"name" : "Iran",
"background" : "https://vfbdt"
"topics" : [
{
"name" : "overview",
"facts" : [
{
"key" : "capital",
"value" : "Kuala Lumpur"
},
{
"key" : "population",
"value" : "232424234"
},
{
"key" : "reliogion",
"value" : "Islam"
}
]
},
{
"name" : "Good to know",
"facts" : [
{
"key" : "key1",
"value" : "value1"
}
]
}
]
}
我想回到上名主題名稱和代碼通過過濾器的所有事實。我寫這篇文章的代碼,但它不能正常工作,並將所有收集只是代碼過濾,並且不考慮topic.name
export function getFactsByCodeAndName(req, res, next) {
Country.find({code:req.params.code,'topics.name':req.params.name})
.exec((err, facts) => {
if (err) return next(err);
return res.status(200).json({
success: true,
message: 'Get country',
data: facts,
});
});
}
它返回「全部收集」還是「全國文件」?假設以後,您可以在應用程序級別按名稱過濾主題。如果出於某種原因想要在數據庫級別執行此操作,則需要通過代碼使用[彙總框架](https://docs.mongodb.com/manual/aggregation/)到'$ match'國家,'$ unwind'主題,'$匹配'他們的名字,'$項目'必填字段。 –