【发布时间】:2016-04-11 05:27:53
【问题描述】:
尽管围绕这个主题有一些问题,但我还没有找到一个明确的通用“最佳实践”来过滤返回的流星 mongo 集合对象。
(仅供参考:我正在使用 MeteorJS)
我从 configs 集合中提取了一个配置文档。
let thisConfig = ClinicConfigs.findOne({_id: "xyz"});
这已返回以下内容
{
_id: "xyz",
name: "john doe's clinic",
activeServices: [
{
name: "teeth whitening",
ref: "teethWhitening",
docs: [
{
docId: "a",
name: "Client questionnaire",
ref: "clientQuestionnaire",
},
{
docId: "b",
name: "Client consent form",
ref: "clientConsentForm",
}
]
},
{
name: "liposuction",
ref: "liposuction",
docs: [
{
docId: "a",
name: "Client questionnaire",
ref: "clientQuestionnaire",
},
{
docId: "b",
name: "Client consent form",
ref: "clientConsentForm",
}
]
}
];
一旦我返回了这个文档/对象,我只需要从 activeServices 数组中提取一个对象。
虽然这行不通,但下面是阐明我需要什么的逻辑:
let thisService = ClinicConfigs.findOne({_id: "xyz"})
.activeServices.findOne({ref: "teethWhitening"});
我尝试了以下方法,但没有成功:
let thisConfig = ClinicConfigs.findOne({_id: "xyz"});
let thisService = thisConfig.activeServices.filter(function(d) {return d.ref === "teethWhitening"})[0];
return thisService.docs;
【问题讨论】:
标签: javascript arrays mongodb meteor ecmascript-6