2016-04-10 9 views
0

ここではテーマを取り上げていますが、返された流星のモンゴルコレクションオブジェクトをフィルタリングするための明確な汎用性のベストプラクティスはまだありません。mongo返されたオブジェクト(findOne)から1つのネストされたオブジェクトをフィルタリングする/返す

(FYI:私はMeteorJSを使用しています)

私はコンフィグコレクションから設定文書を引っ張ってきました。私はactiveServices配列からただ一つのオブジェクトを引く必要があり、このドキュメント/オブジェクトを返したら

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", 
       } 
      ] 
     } 
    ]; 

を返しました。

、これは動作しませんが、ここで私は必要なものを明確にするロジックがあります:

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; 

答えて

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"); 

そしてそれが取り出されます。

+1

質問を入力するときに最後の括弧が誤って入力されました。あなたのソリューションは独立して動作しますが、私のMeteorヘルパー機能内では使用されません。これは私がこれがタイミングの問題であると信じさせる。あなたの助けをありがとう、私は今私が使用していた機能を排除しました! – Aero

関連する問題