【问题标题】:Mongo aggregate rename field with $project?Mongodb 使用 $project 聚合重命名字段?
【发布时间】:2020-08-21 21:35:21
【问题描述】:

我正在尝试生成聚合结果,但重命名了一些字段:

db.articles.aggregate([
    {$match: {
        "model.lang": "en"
    }},
    {$project: {
        "_id": 0,
        "model.title": 1,
        "model.address_en": "$address",
        "model.date": { $dateToString: { format: "%Y-%m-%d", date: "$date" } }
    }}
]);

如您所见,我正在尝试将“model.title”重命名为“title”,将“model.address_en”重命名为“address”,并将“model.date”重命名为“date”……但运气不佳:

{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }

我做错了什么?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    给定articles的文档

    { "_id" : ObjectId("56cea763d43e3500f6768482"), 
      "name" : "aaa", 
      "model" : { "lang" : "en", 
                   "title" : "b", 
                   "date" : ISODate("2016-02-25T07:04:03.414Z") 
                 }, 
               }
    

    将“model.title”重命名为“title”,将“model.address_en”重命名为“address”,将“model.date”重命名为“date”

    db.articles.aggregate([
                 {$match: {'model.lang': 'en'}}, 
                 {$project: {
                              _id: 0, 
                              'title': '$model.title', 
                              'date': {$dateToString: {format: '%Y-%m-%d', 
                                                       date: '$model.date'}}}}])
    

    结果是

    { "date" : "2016-02-25", "title" : "b" }
    { "date" : "2016-02-25", "title" : "c" }
    

    【讨论】:

    • 问题中的"model" 字段不是数组,否则投影尝试将在输出中显示数组括号[]。所以这实际上只是他们错误的“左侧”到“右侧”部分。
    【解决方案2】:

    你可以用这个

    db.articles.aggregate([
        {
            $match: {
                "model.lang": "en"
            }
        },
        {
            $project: {
                "_id": 0,
                "title": "$model.title",
                "address": "$model.address_en",
                "date": {
                    $dateToString: { 
                        format: "%Y-%m-%d", date: "$model.date" 
                    }
                }
            }
        }
    ]);
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    【解决方案3】:

    回答

    andranikasl answer

    解释

    您的代码与您所说的相反。 我将在您的代码中解释它。

    db.articles.aggregate([
        {$match: {
            "model.lang": "en"
        }},
        {$project: {
            "_id": 0,
            "model.title": 1, // Wanted: title <- model.title // Result: model.title <- model.title // it should show you title inside model object
            "model.address_en": "$address", // Wanted: address <- model.address_en // Result: model.address_en <- address // address doesn't exist! thats why it shows nothing
            "model.date": { $dateToString: { format: "%Y-%m-%d", date: "$date" } } // Wanted: date <- model.date // Result: model.date <- date // date doesn't exist! thats why it shows nothing
        }}
    ]);
    

    当您要包含的字段不存在时,$project ignores it,这就是您在输出中看不到任何所需值的原因。

    【讨论】:

    • 作为对另一个答案的评论,这会更有意义。
    • 是的,你是对的,但我无法评论,我的声誉低于 50。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多