【问题标题】:MongoDB query with projection without nested field that has no fixed parent name带有没有固定父名称的嵌套字段的投影的 MongoDB 查询
【发布时间】:2020-09-21 17:51:11
【问题描述】:

我正在尝试通过使用投影的查询从 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"
        }
    }
}

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

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

我的猜测是某种通配符可以完成这项工作,但我找不到。

我试过了:、$、$、$**、** 但没有成功。

【问题讨论】:

    标签: python mongodb nosql


    【解决方案1】:
    //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"
                    }
            }
    }
    >
    

    【讨论】:

    • @Johnnes Souza,如果它解决了您的问题,请将其标记为正确答案。正确答案旁边会显示一个绿色勾号。
    • 这个解决方案确实是我所要求的,但最终它比带来'class'字段要慢得多。无论如何tks!
    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2021-01-28
    • 2020-10-06
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多