带有投影的MongoDB查询,没有固定父名称的嵌套字段

2024-10-03 02:37:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过使用投影的查询从MongoDB检索一些文档

该文档如下所示:

{
    "_id": "01",
    "country": "EUA",
    "created": "2020-09-10T18:12:20.649Z",
    "products": {
        "0001": {
            "id": "0001",
            "price": "1.25",
            "timestamp": "16004443546",
            "class": "com.website.ecommerce.src.main.java.model.product"
        },
        "0123": {
            "id": "0123",
            "price": "1.50",
            "timestamp": "16004443546",
            "class": "com.website.ecommerce.src.main.java.model.product"
        },
        "0443": {
            "id": "00443",
            "price": "1.75",
            "timestamp": "16004443546",
            "class": "com.website.ecommerce.src.main.java.model.product"
        }
    }
}

我不需要检索“class”字段,因此给定一个包含10k+结果的查询,该字段代表响应大小的很大一部分

collection.find({'_id': some_id}, {'products.*._class': 0 })

我的猜测是有某种通配符可以完成任务,但我无法找到

我试过:,$,$,$**** 但是没有成功


Tags: 文档srccomidmodelmainwebsitejava
1条回答
网友
1楼 · 发布于 2024-10-03 02:37:16
//actual code output from mongoshell 4.2.6 on windows
//prepare the document in a collection called eua, as given in problem statement
> db.eua.find().pretty();
{
        "_id" : "01",
        "country" : "EUA",
        "created" : "2020-09-10T18:12:20.649Z",
        "products" : {
                "0001" : {
                        "id" : "0001",
                        "price" : "1.25",
                        "timestamp" : "16004443546",
                        "class" : "com.website.ecommerce.src.main.java.model.product"
                },
                "0123" : {
                        "id" : "0123",
                        "price" : "1.50",
                        "timestamp" : "16004443546",
                        "class" : "com.website.ecommerce.src.main.java.model.product"
                },
                "0443" : {
                        "id" : "00443",
                        "price" : "1.75",
                        "timestamp" : "16004443546",
                        "class" : "com.website.ecommerce.src.main.java.model.product"
                }
        }
}
//use aggreate project command to first convert object to array in first stage
// use the project in 2nd stage to hide the class field
// reconvert back to original array to object with required fields marked as 1
> db.eua.aggregate([
...
... {
...     $project: {
...       _id: 1,
...       country: 1,
...       created: 1,
...       prodToArray: {
...         $objectToArray: "$products"
...       }
...     }
...   },
...   {
...     $project: {
...       "prodToArray.v.class": 0
...     }
...   },
...   {
...       $project:{
...           _id: 1,
...           country: 1,
...           created: 1,
...           products:{
...               $arrayToObject:"$prodToArray"
...           }
...       }
...   }
... ]).pretty()
{
        "_id" : "01",
        "country" : "EUA",
        "created" : "2020-09-10T18:12:20.649Z",
        "products" : {
                "0001" : {
                        "id" : "0001",
                        "price" : "1.25",
                        "timestamp" : "16004443546"
                },
                "0123" : {
                        "id" : "0123",
                        "price" : "1.50",
                        "timestamp" : "16004443546"
                },
                "0443" : {
                        "id" : "00443",
                        "price" : "1.75",
                        "timestamp" : "16004443546"
                }
        }
}
>

相关问题 更多 >