2016-07-21 7 views
3

に存在している私は私のデシベルコレクションにfolowingオブジェクト構造を持っている:

{ 
    "name" : "test", 
    "code" : "test", 
    "attributes" : [ 
     { 
      "name" : "test1", 
      "code" : "code1" 
     }, 
     { 
      "name" : "test2", 
      "code" : "code2", 
      "value" : true 
     }, 
     { 
      "name" : "test3", 
      "code" : "code3", 
      "value" : "" 
     }, 
     { 
      "name" : "test4", 
      "code" : "code4" 
      "value" : [ 
       { 
        "code" : "code4.1", 
        "name" : "test4.1" 
       }, 
       { 
        "name" : "test4.2" 
       } 
      ] 
     } 
    ] 
} 

だから、「値」プロパティが空の文字列、ブール、配列あるいはすべてで定義されていないことができます。

空ではない属性配列を持つオブジェクトをリストするクエリを作成するにはどうすればいいですか?配列内の少なくとも1つのオブジェクトの内部に "attributes.value"プロパティが定義されていませんか?

p.s.次のクエリを試しました:

db.collection.find({"attributes": {$exists: true, $ne: []}, "attributes.value": {$exists: false}}) 

クエリ結果は空です。

答えて

5

$ elemMatch演算子は、指定されたすべてのクエリ 基準に一致した少なくとも一つの要素を持つ配列フィールド を含む文書が一致します。私にとって

このクエリの作品:

db.getCollection('testeur').find({ "attributes": { 
     $exists: true, 
     $ne: [], 
     $elemMatch: { "value": {$exists: false } } 
    } 
}) 
+0

おかげで、それは私が探していたまさにでした。 –

関連する問題