Mongo使用$ project聚合重命名字段?

时间:2016-02-25 06:48:11

标签: mongodb

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

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" to" title"," model.address_en"到"地址"和" model.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 } }

我做错了什么?

3 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:3)

您可以使用此

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" 
                }
            }
        }
    }
]);

答案 2 :(得分:1)

答案

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,这就是为什么在输出中看不到任何想要的值的原因。