Mongodb:如何获取子集合列表

时间:2015-11-04 12:17:41

标签: 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所有成员的所有“想要”(并非所有文件)都是“Ha Noi”

1 个答案:

答案 0 :(得分:2)

我认为您正在寻找的答案是

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

如果您只需输出“wantList”,则应尝试此查询

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

这将仅输出“wantList”数组,其中一个省份等于“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 } ] } }, { "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 } ] } } }

我认为这是您正在寻找的答案。