简体   繁体   English

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

[英]MongoDB query with projection without nested field that has no fixed parent name

I'm trying to retrieve some documents from MongoDB through a query using projection.我正在尝试通过使用投影的查询从 MongoDB 检索一些文档。

The document looks something like this:该文档看起来像这样:

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

I don't need the "class" field to be retrieved, so given a query with 10k+ results, this field represents a big part of the response size.我不需要检索“类”字段,因此给定一个包含 10k+ 个结果的查询,该字段代表响应大小的很大一部分。

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

My guess is that there's some kind of wildcard character will do the job, but I'm unabled to find.我的猜测是有某种通配符可以完成这项工作,但我找不到。

I tried: , $, $ , $**, ** but no success.我试过: , $, $ , $**, ** 但没有成功。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM