2015-11-04 76 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