2015-11-04 71 views
3

的名單我有一個成員的收集與以下數據:MongoDB的:如何獲得子集合

db.member.insert(
{ 
    userName: "TanNM", 
    password: "xxx", 
    wantList: [{ 
     title: "Want 1.1 - HN", 
     description: "Want 1.1 description", 
     province:{ 
      name: "Ha Noi", 
      districtList:[ { name: "Ha Dong", qty: 25 }, { name: "Ba Dinh", qty: 50 } , { name: "Cau Giay", qty: 25 }, { name: "Hoan Kiem", qty: 50 } ] 
     } 
    }, { 
     title: "Want 1.2 - HN", 
     description: "Want 1.2 description", 
     province:{ 
      name: "SG", 
      districtList:[ { name: "Ha Dong", qty: 25 }, { name: "Ba Dinh", qty: 50 } , { name: "Cau Giay", qty: 25 }, { name: "Hoan Kiem", qty: 50 } ] 
     } 
    }], 
    stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ], 
    category: "clothing" 
}) 

db.member.insert(
{ 
    userName: "MinhNN", 
    password: "xxx", 
    wantList: [{ 
     title: "Want 2.1 - HN", 
     description: "Want 2.1 description", 
     province:{ 
      name: "Ha Noi", 
      districtList:[ { name: "Ha Dong", qty: 25 }, { name: "Ba Dinh", qty: 50 } , { name: "Cau Giay", qty: 25 }, { name: "Hoan Kiem", qty: 50 } ] 
     } 
    }, {title: "Want 2.2 - HN", 
     description: "Want 2.2 description", 
     province:{ 
      name: "Ha Noi", 
      districtList:[ { name: "Ha Dong", qty: 25 }, { name: "Ba Dinh", qty: 50 } , { name: "Cau Giay", qty: 25 }, { name: "Hoan Kiem", qty: 50 } ] 
     } 
    }], 
    stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ], 
    category: "clothing" 
}) 

db.member.insert(
{ 
    userName: "DungNP", 
    password: "xxx", 
    wantList: { 
     title: "Want 3 - SG", 
     description: "Want 3 description", 
     province:{ 
      name: "TP Ho Chi Minh", 
      districtList:[ { name: "Ha Dong", qty: 25 }, { name: "Ba Dinh", qty: 50 } , { name: "Cau Giay", qty: 25 }, { name: "Hoan Kiem", qty: 50 } ] 
     } 
    }, 
    stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ], 
    category: "clothing" 
}) 

會員有一些人想(這個wantlist),想在一個省/區。

如何讓所有的「要」與province.name全體成員(並非所有文件)爲「河內」

+1

嗨!我刪除了我的答案,因爲它不能解決您的問題。我終於明白了,這對我來說目前有點棘手。當我有時間的時候,我會試着去挖掘一下,但是我希望有人會回答你的問題。 :) 祝你好運! – Askar

+0

非常感謝! @oscar。 – Parabol

回答

2

我認爲你正在尋找的答案是這樣的

db.member.find({"wantList.province.name": "Ha Noi"}); 

如果你只需要輸出「這個wantlist」,你應該嘗試此查詢

db.member.find({"wantList.province.name": "Ha Noi"}, {"wantList": 1}); 

這隻會輸出「這個wantlist」數組,其中的省份之一等於「河內」。我希望這是你正在尋找的答案。

第二個查詢返回如下:

{ "_id" : ObjectId("56568bbd2688b376a56878c5"), "wantList" : [ { "title" : "Want 1.1 - HN", "description" : "Want 1.1 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } }, { "title" : "Want 1.2 - HN", "description" : "Want 1.2 description", "province" : { "name" : "SG", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } ] } 

{ "_id" : ObjectId("56568bc72688b376a56878c6"), "wantList" : [ { "title" : "Want 2.1 - HN", "description" : "Want 2.1 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } }, { "title" : "Want 2.2 - HN", "description" : "Want 2.2 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } ] } 

編輯:

我認爲你正在尋找這樣的回答:

db.member.find(
    {"wantList.province.name": "Ha Noi"}, 
    {"wantList": {"$elemMatch": {"province.name": "Ha Noi"}}} 
); 

將返回:

{ "_id" : ObjectId("56568bbd2688b376a56878c5"), "wantList" : [ { "title" : "Want 1.1 - HN", "description" : "Want 1.1 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } ] } 
{ "_id" : ObjectId("56568bc72688b376a56878c6"), "wantList" : [ { "title" : "Want 2.1 - HN", "description" : "Want 2.1 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } ] } 

如果你想重新移動_id你剛纔輸入以下查詢:

db.member.find(
    {"wantList.province.name": "Ha Noi"}, 
    {"wantList": {"$elemMatch": {"province.name": "Ha Noi"}}, "_id": 0} 
); 

編輯2:

我覺得是需要聚合解決您的問題。試試這個查詢。

db.member.aggregate([ 
    {"$match": {"wantList.province.name": "Ha Noi"}}, 
    {"$unwind": "$wantList"}, 
    {"$match": {"wantList.province.name": "Ha Noi"}}, 
    {"$project": {"_id": 0, "wantList": 1}} 
]); 

將返回:

{ "wantList" : { "title" : "Want 1.1 - HN", "description" : "Want 1.1 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } } 
{ "wantList" : { "title" : "Want 2.1 - HN", "description" : "Want 2.1 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } } 

{ "wantList" : { "title" : "Want 2.2 - HN", "description" : "Want 2.2 description", "province" : { "name" : "Ha Noi", "districtList" : [ { "name" : "Ha Dong", "qty" : 25 }, { "name" : "Ba Dinh", "qty" : 50 }, { "name" : "Cau Giay", "qty" : 25 }, { "name" : "Hoan Kiem", "qty" : 50 } ] } } } 

我認爲這是你正在尋找的答案。

+0

不,我檢查了它沒有回答的問題,你有驗證結果嗎? – VedX

+0

爲什麼它不是答案。最後一個查詢僅返回省名Ha Noi的wantList。你能指出爲什麼這不是正確的答案嗎? – suecarmol

+0

我可能是錯的對不起,但請您在回答中添加該查詢的結果。 – VedX