【问题标题】:Filtering/Returning a single nested object from mongo returned object (findOne)从 mongo 返回的对象(findOne)中过滤/返回单个嵌套对象
【发布时间】: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


    【解决方案1】:

    它有效,但我必须用尾括号更正您的 thisConfig 数据对象。所以一旦它就像

    var thisCongig = {
        _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",
                     }
                 ]
             }
        ]
    };
    var thisService = data.activeServices.find( d => d.ref == "teethWhitening");
    

    它被检索了。

    【讨论】:

    • 结束大括号是我输入问题时的错字。您的解决方案是孤立的,但在我的 Meteor 辅助函数中使用时不能。这让我相信这是一个时间问题。感谢您的帮助,我现在已经排除了我正在使用的功能!
    猜你喜欢
    • 2020-11-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多