Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo aggregate rename field with $project?

Tags:

I'm trying to generate a aggregate result but rename some fields:

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

As you can see I'm trying to rename "model.title" to "title", "model.address_en" to "address", and "model.date" to "date" .. without much luck though:

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

What am I doing wrong?

like image 476
Martyn Avatar asked Feb 25 '16 06:02

Martyn


People also ask

How do I rename a field in MongoDB aggregation?

Introduction to the MongoDB $rename operator In this case, you can use the $rename operator. The $rename is a field update operator that allows you to rename a field in a document to the new one. The $rename operator has the following syntax: { $rename: { <field_name>: <new_field_name>, ...}}

How do I project specific fields in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

What does $project do in MongoDB?

The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields. Specifies the inclusion of a field.

Why are my MongoDB field names so inconsistent?

Sometimes when you query a collection in MongoDB, you might not be happy with the field names. By default, the field names are simply a reflection of the field names in the actual documents. Perhaps the field names are inconsistent, or there’s a typo.

How to view aggregation query’s in mongo shell code?

Click on the play button under Stage Output to execute just this stage. Or execute the entire pipeline by clicking on the play button in the toolbar. The pipeline and stage outputs should now only show the fields we want. To view the aggregation query’s in mongo shell code, click on Query Code and choose mongo shell from the dropdown.

How do I suppress a field in a MongoDB document?

You can use the variable REMOVE in aggregation expressions to conditionally suppress a field. For an example, see Conditionally Exclude Fields. MongoDB also provides $addFields to add new fields to the documents. To add a new field or to reset the value of an existing field, specify the field name and set its value to some expression.

How do I rename a field in a query?

By default, the field names are simply a reflection of the field names in the actual documents. Perhaps the field names are inconsistent, or there’s a typo. Whatever the reason, you can use the $project aggregation pipeline stage to rename a field in your query results.


2 Answers

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" }
like image 50
zangw Avatar answered Sep 20 '22 07:09

zangw


You can just use this

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" 
                }
            }
        }
    }
]);
like image 45
andranikasl Avatar answered Sep 22 '22 07:09

andranikasl