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?
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>, ...}}
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
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.
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.
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.
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.
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.
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" }
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"
}
}
}
}
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With