2017-10-13 53 views
1

我有以下文件:如何獲得蒙戈子文檔記錄外殼

"Demo" : { 
      "SI" : { 
       "Value1" : 40, 
       "Value2" : [ 
        10, 
        15, 
        20 

       ] 
      } , 
      "RS" : { 
       "Value1" : 4, 
       "Value2" : [ 
        1, 
        2, 
        3, 
        4 
       ] 
       } 
    } 

我要爲子文檔「SI」獲取數據。我嘗試過以下查詢: db.getCollection('input').find({"Demo.SI":"SI"}),但它沒有給出'SI'文檔的任何記錄。期望的輸出是:

"SI" : { 
      "Value1" : 40, 
      "Value2" : [ 
       10, 
       15, 
       20 

      ] 
     } 

請指定查詢出錯的位置。

回答

1

首先檢查SI存在或不使用$exists然後將其添加在projection如下的文件:

db.input.find({"Demo.SI":{"$exists":true}},{"Demo.SI":1,"_id":0}).pretty() 
0
db.collection.find({ "Demo.SI": { $exists: true, $ne: null } },{"Demo.SI":1,"_id":0}) 

這個查詢將返回所有具有SI關鍵