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

3
我想要找到所有属于卖家ID的数据,但不包括其他卖家ID。
例如:seller_id :1001,我想要所有属于此卖家ID的数据,包括c_name,_id,c_order,但不包括seller_id:1000,1002等。
{
    "_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"
}

你应该尝试使用“聚合”来完成这个任务。你能展示一下你尝试过什么吗? - Shrabanee
1
继@titi23之后,阅读https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/。希望这能帮助您编写查询。 - KaSh
1个回答

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

更加具体地说,使用项目db.getCollection('Mytest').aggregate([{"$unwind":"$c_order"},{"$match":{"c_order.seller_id":"1000"}}, {$project:{"c_name":1,"c_order.product_id":1,"c_order.product_name":1,"c_order.price":1}} ])。 - Shantanu Madane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接