如何在mongodb中查找子文档中的数据

时间:2016-06-16 08:44:06

标签: mongodb mongodb-query aggregation-framework

我想查找所有属于卖家ID的数据,不包括其他seller_id。

例如:seller_id :1001,我希望c_name,_id,c_order排除seller_id:1000,1002等所有属于此卖家ID的数据。

{
    "_id" : ObjectId("575e83376a6f714301dd4693"),
    "c_name" : "Amit",
    "c_order" : [
        {
            "seller_id" : "1000",
            "product_id" : "2000",
            "product_name" : "iphone 5s",
            "price" : "22000"
        },
        {
            "seller_id" : "1001",
            "product_id" : "2001",
            "product_name" : "iphone 6s",
            "price" : "42000"
        },
        {
            "seller_id" : "1002",
            "product_id" : "2003",
            "product_name" : "laptop",
            "price" : "28000"
        }
    ],
    "customer_id" : "21"
},
{
    "_id" : ObjectId("575e838f6a6f714301dd4694"),
    "c_name" : "Prabal",
    "c_order" : [
        {
            "seller_id" : "1004",
            "product_id" : "2004",
            "product_name" : "belt",
            "price" : "2000"
        },
        {
            "seller_id" : "1001",
            "product_id" : "2005",
            "product_name" : "coffee mug",
            "price" : "400"
        },
        {
            "seller_id" : "1002",
            "product_id" : "2006",
            "product_name" : "pen drive",
            "price" : "800"
        }
    ],
    "customer_id" : "22"
},
{
    "_id" : ObjectId("575e894c6a6f714301dd4695"),
    "c_name" : "Raman",
    "c_order" : [
        {
            "seller_id" : "1001",
            "product_id" : "2004",
            "product_name" : "belt",
            "price" : "2000"
        },
        {
            "seller_id" : "1001",
            "product_id" : "2005",
            "product_name" : "coffee mug",
            "price" : "400"
        },
        {
            "seller_id" : "1002",
            "product_id" : "2006",
            "product_name" : "pen drive",
            "price" : "800"
        }
    ],
    "customer_id" : "22"
}

1 个答案:

答案 0 :(得分:1)

这对你有用吗?

db.getCollection('collectionName').aggregate([

    { "$unwind" : "$c_order" },
    { "$match" : { "c_order.seller_id" : "1001"}}

])

您应该获得以下输出:

/* 1 */
{
    "_id" : ObjectId("575e83376a6f714301dd4693"),
    "c_name" : "Amit",
    "c_order" : {
        "seller_id" : "1001",
        "product_id" : "2001",
        "product_name" : "iphone 6s",
        "price" : "42000"
    },
    "customer_id" : "21"
}

/* 2 */
{
    "_id" : ObjectId("575e838f6a6f714301dd4694"),
    "c_name" : "Prabal",
    "c_order" : {
        "seller_id" : "1001",
        "product_id" : "2005",
        "product_name" : "coffee mug",
        "price" : "400"
    },
    "customer_id" : "22"
}

/* 3 */
{
    "_id" : ObjectId("575e894c6a6f714301dd4695"),
    "c_name" : "Raman",
    "c_order" : {
        "seller_id" : "1001",
        "product_id" : "2004",
        "product_name" : "belt",
        "price" : "2000"
    },
    "customer_id" : "22"
}

/* 4 */
{
    "_id" : ObjectId("575e894c6a6f714301dd4695"),
    "c_name" : "Raman",
    "c_order" : {
        "seller_id" : "1001",
        "product_id" : "2005",
        "product_name" : "coffee mug",
        "price" : "400"
    },
    "customer_id" : "22"
}