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:[ { 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"
})

成员(member)有一些旺旺(wantList),某省/区旺旺。

如何获取所有成员的所有“想要”(不是所有文件)province.name 是“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 } ] } } ] }

如果您想删除 _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 } ] } } }

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

关于Mongodb:如何获取子集合列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521716/

相关文章:

macos - 蒙戈 : command not found on Mac OSX even though it's in the PATH

mongodb - 连接到主机时出错 : could not connect to server: server selection error: server selection timeout current topology: Type: Single Servers

java - mongodb 驱动程序 - 如何使用 log4j 隐藏调试日志记录?

javascript - node.js - 发布响应不等待回调完成

java - 如何使用 java 驱动程序复制 mongodb 集合 (v3.4)

MongoDB 如何找到包含指定点的多边形?

node.js - 当数据量很大时,Angular 2 http get 请求被调用两次

macos - 安装 MongoDB

mongodb - 带 OR 条件的 mongo 查询说明

mongodb - 与Mongo Atlas的连接可使用家庭无线功能,但不能使用电话连接