如何解决MongoDB嵌套搜索,项目部分结果
我有一个类似这样的模型,它基于来自多个其他SO问题的建议,利用数组格式的多语言:
Exercise Schema
----------------
[{
_id: "5f626ca1fcfe0005857e823f",Image: "image url",...ExtraDetails,Translations: [
{
_id: "5f626ca2fcfe0005857e870b",Language: "en",Name": "Push Ups"
},{
_id: "5f626ca2fcfe0005857e870c",Language: "no",Name: "Armhevinger"
}
]
},{
_id: "5f626ca1fcfe0005857e82c9",Translations: [
{
_id: "5f626ca2fcfe0005857e8795",Name": "Weighted Push Ups"
},{
_id: "5f626ca2fcfe0005857e8796",Name: "Armhevinger med vekt"
}
]
}]
我已将此索引添加到数据库:
db.exercises.createIndex({ "Translations.Name": "text" });
我现在想要做的是根据language参数进行查询和返回,并由用户使用仅某些字段进行搜索。让我举个例子:
语言=“ en” 搜索=“推”
我希望它返回:
[{
_id: "5f626ca1fcfe0005857e823f",Translations: [
{
Name": "Push Ups"
}
]
},Translations: [
{
Name": "Weighted Push Ups"
}
]
}]
因此,我只想返回正确的转换,并且只返回其中的某些字段,然后还限制该特定嵌套对象之外的其他数据。
const exercises = await Exercise.find({ '$text': { '$search': 'push' } },{ Image: 1,Translations: 1 }).sort({ _id: 1 });
但是这仍然返回所有翻译,以及每个翻译中的额外数据。有人有什么想法/建议吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。