从mongo返回的对象中过滤/返回单个嵌套对象(findOne)

时间:2016-04-10 21:00:34

标签: javascript arrays mongodb meteor ecmascript-6

虽然这里有关于这个主题的问题,但我还没有找到一个明确的多功能“最佳实践”来过滤返回的流星蒙戈收集对象。

(仅供参考:我正在使用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;

0 个答案:

没有答案