2016-06-10 42 views
0

我有表示構成這樣查詢文檔可能不存在

{ 
    "id" : "34534", 
    "name" : "rule1", 
    "parameters" : { 
     "nodes" : [ 
      { 
       "type" : "x", 
       "properties" : { 
        "dataSets" : [ 
         { 
          "dataSetKey" : "key" 
         }, 
         { 
          "dataSetKey" : "key2" 
         } 
        ], 
        "resource" : { 
         "id" : "4353454" 
        } 
       } 
      }, 
      { 
       "type" : "y", 
       "params" : {}   
      } 
     ] 
    } 
} 

我想要寫的查詢,其返回整個文檔的規則的文檔,如果它有類型x的節點,並且在該節點中它具有dataSetKey爲「key」的dataSet。除非節點類型爲x,否則它不會有包含dataSets數組的屬性字段。我試過此查詢,但它返回0文件

db.getCollection('Rules').find({ 
    "parameters.nodes": { 
     $elemMatch: { 
      "type": "x", 
      "properties.dataSets": { 
       $elemMatch: { 
        "dataSetKey": "key" 
       } 
      } 
     } 
    } 
}) 

我也試過這個

db.getCollection('Rules').find({"parameters.nodes.properties.datasets": {$exists: true}},{ 
    "parameters.nodes": { 
     $elemMatch: { 
      "type": "x", 
      "properties.dataSets": { 
       $elemMatch: { 
        "dataSetKey": "key" 
       } 
      } 
     } 
    } 
}) 

但它返回下面的錯誤。

"$err" : "Cannot use $elemMatch projection on a nested field (currently unsupported)." 

任何幫助,非常感謝。

回答

1

我不知道它是否需要對mongo版本做任何事情?我運行了你的第一個查詢,並得到了1個文檔。