Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb:如何获取子集合列表_Mongodb - Fatal编程技术网

Mongodb:如何获取子集合列表

Mongodb:如何获取子集合列表,mongodb,Mongodb,我有一个包含以下数据的成员集合: db.member.insert( { userName: "TanNM", password: "xxx", wantList: [{ title: "Want 1.1 - HN", description: "Want 1.1 description", province:{ name: "Ha Noi", districtList:[ {

我有一个包含以下数据的成员集合:

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),需要在省/区


如何获得所有
省成员的所有“想要的”(而不是所有文档)。名称是“Ha-Noi”

我想你想要的答案是

db.member.find({"wantList.province.name": "Ha Noi"});
如果您只需要输出“wantList”,则应尝试此查询

db.member.find({"wantList.province.name": "Ha Noi"}, {"wantList": 1});
这将只输出其中一个省等于“Ha Noi”的“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 } ] } } ] }
{ "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 } ] } } }
如果要删除_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}}
]);
返回:

{ "_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 } ] } } ] }
{ "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 } ] } } }

我想这就是你想要的答案。

嗨!我已经删除了我的答案,因为它不能解决你的问题。我终于明白你的意思了,这对我来说现在有点棘手。当我有时间的时候,我会尽力挖掘一下,但我希望有人能回答你的问题祝你好运非常感谢@奥斯卡:不,我查过了,它没有回答问题,你们验证过结果吗?为什么它不是答案。最后一个查询只返回省份名称为Ha Noi的wantList。你能说明为什么这不是正确的答案吗?我可能错了,对不起,但请你在答案中添加该查询的结果。只添加了答案。让我知道这是否是你想要的答案,或者给我一个你想要答案的例子。嗨@suecarmol,我测试了你的答案。但它没有显示带有wantList.title的wantList:“想要2.2-HN”。也许它少了点什么。我的问题差不多有答案了。