Mongo aggregate rename field with $project?

Given the document of articles

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

Rename “model.title” to “title”, “model.address_en” to “address”, and “model.date” to “date” through

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

The result is

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)