2012-02-15 55 views
5
{ 
    "_id":{ 
     "oid":"4f33bf69873dbc73a7d21dc3" 
    }, 
    "country":"IND", 
    "states":[{ 
      "name":"orissa", 
      "direction":"east", 
      "population":41947358, 
      "districts":[{ 
        "name":"puri", 
        "headquarter":"puri", 
        "population":1498604 
       }, 
       { 
        "name":"khordha", 
        "headquarter":"bhubaneswar", 
        "population":1874405 
       } 
      ] 
     }, 
     { 
      "name":"andhra pradesh", 
      "direction":"south", 
      "population":84665533, 
      "districts":[{ 
        "name":"rangareddi", 
        "headquarter":"hyderabad", 
        "population":3506670 
       }, 
       { 
        "name":"vishakhapatnam", 
        "headquarter":"vishakhapatnam", 
        "population":3789823 
       } 
      ] 
     } 
    ] 
} 

在上述集合(即國家)我只有一個文件,我想獲取有關特定狀態的詳細信息(可以說「country.states.name」:「奧里薩邦」) ,但我想我的結果作爲下在這裏,而不是整個文件。是那裏莫戈的方式...只選擇子文檔或陣列

 { 
    "name": "orissa", 
    "direction": "east", 
    "population": 41947358, 
    "districts": [ 
     { 
      "name": "puri", 
      "headquarter": "puri", 
      "population": 1498604 
     }, 
     { 
      "name": "khordha", 
      "headquarter": "bhubaneswar", 
      "population": 1874405 
     } 
    ] 
    } 

感謝

回答

2

MongoDB中的任何查詢總是返回根文檔。

只有一個方法可以讓你通過$slice加載與父母一個子文件,如果你知道在嵌套數組狀態的序號:按默認順序

// skip ordinalNumberOfState -1, limit 1 
db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}}) 

$切片工作(如文檔在插入嵌套數組)。 此外,如果你沒有一個國家需要的字段,你可以只包括_id和結果指出:

db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}, _id: 1}) 

然後導致文檔將看起來像這樣的:

{ 
    "_id":{ 
     "oid":"4f33bf69873dbc73a7d21dc3" 
    }, 
    "states":[{ 
      "name":"orissa", 
      "direction":"east", 
      "population":41947358, 
      "districts":[{ 
        "name":"puri", 
        "headquarter":"puri", 
        "population":1498604 
       }, 
       { 
        "name":"khordha", 
        "headquarter":"bhubaneswar", 
        "population":1874405 
       } 
      ] 
     }] 
} 
+0

那麼我們可以在Mongo中編寫一個程序,我們可以先獲得狀態的序號然後使用$ slice? – John 2012-02-15 10:24:09

+0

@John:不,但可以在將國家插入數據庫時​​將'id'添加到每個狀態等於序號的狀態。然後使用這個ID來加載特定的狀態。 – 2012-02-15 10:34:28

5

你不能這樣做它現在,但你將能夠與$放鬆在aggregation framework。您現在可以使用實驗2.1分支進行試用,穩定版本將在2.2版本中推出,可能在幾個月內推出。

+0

繼承人與此相關的傑拉票:https://jira.mongodb.org/browse/SERVER-828 – Ross 2012-02-15 09:52:07

7

嘗試這樣:

db.countries.aggregate(  
    { 
     "$project": { 
      "state": "$states", 
      "_id": 0 
     } 
    }, 
    { 
     "$unwind": "$state" 
    }, 
    { 
     "$group": { 
      "_id": "$state.name", 
      "state": { 
       "$first": "$state" 
      } 
     } 
    }, 
    { 
     "$match": { 
      "_id": "orissa" 
     } 
    } 
); 

,並得到:

{ 
    "result" : [ 
      { 
        "_id" : "orissa", 
        "state" : { 
          "name" : "orissa", 
          "direction" : "east", 
          "population" : 41947358, 
          "districts" : [ 
            { 
              "name" : "puri", 
              "headquarter" : "puri", 
              "population" : 1498604 
            }, 
            { 
              "name" : "khordha", 
              "headquarter" : "bhubaneswar", 
              "population" : 1874405 
            } 
          ] 
        } 
      } 
    ], 
    "ok" : 1 
1
db.countries.find({ "states": { "$elemMatch": { "name": orissa }}},{"country" : 1, "states.$": 1 }) 
0

如果你不想使用aggregate,你可以在應用層使用下劃線做到這一點很容易地(默認包括):

var country = Groops.findOne({"property":value); 
var state _.where(country, {"state":statename}); 

這會給你匹配statename的整個狀態記錄。很方便。