38

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?

0

3 Answers 3

50

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" } 
Sign up to request clarification or add additional context in comments.

1 Comment

The "model" field in the question would not be an array, otherwise the attempts at projecting would show array brackets [] in the output. So it's really just the "left-side" to "right-side" part they have wrong.
25

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

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
3

Answer

andranikasl answer

Explain

Your code does the opposite of what you say it does. I will explain it inside of your code.

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

When a field you want to include does not exist, $project ignores it and that's why you don't see any wanted value in your output.

2 Comments

This would make more sense as a comment on the other answer.
Yes you're right, but I can't comment, my reputation is below 50.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.