Niraj Kumar
Niraj Kumar

Reputation: 38

How to find the data from sub document in mongodb

I want to find the data which all belong to seller id,excluding other seller_id.

For example :seller_id :1001, i want all the data belong to this seller id long with c_name,_id,c_order excluding seller_id:1000,1002 etc.

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

Upvotes: 3

Views: 65

Answers (1)

Andy Jenkins
Andy Jenkins

Reputation: 647

Does this work for you?

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

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

])

You should get the following output:

/* 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"
}

Upvotes: 1

Related Questions