0

I use query such as:

db.users.find({}).projection({"_id":0,"email":1,"name.fullName":1})

Which returns me output such as:

{ 
    "email" : "foo@example.bar", 
    "name" : {
        "fullName" : "Foo Bar"
    }
}

I am trying to convert such object to be:

{ 
    "email" : "foo@example.bar", 
    "name" : "Foo Bar"
}
Kunok
  • 8,089
  • 8
  • 48
  • 89
  • 2
    You can use `db.users.aggregate({$project:{"_id":0,"email":1,"name":"$name.fullName"})` for transforming output. – s7vr Mar 07 '17 at 14:08
  • @Veeram Yes, that's what I am looking for, please post it as an answer – Kunok Mar 07 '17 at 14:12

1 Answers1

4

You have to use $project stage of aggregation which takes a key and value ( as expression ) to transform the output.

db.users.aggregate({$project:{"_id":0,"email":1,"name":"$nam‌​e.fullName"}
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Hi @Veeram. Can you please avoid answering clear dupes as it distributes content all over the place. Imagine if I'm looking for an answer to a question, and I find out that my question has been asked 5 times already, should I have to look through all the answers on all 5 questions to find the best one? It is much easier and more efficient to have just one question with all of the answers ranked against one another by vote. – chridam Mar 07 '17 at 14:28
  • @chridam The problem is that in some cases there are many ways or no standard way to ask question for the same problem. For example, I tried to search for this question and I ended up in sea of questions related to `$toUpper` which is not relevant here. – Kunok Mar 07 '17 at 14:32
  • @chridam I agree its a problem. Thank you for taking time to comment on it. I know there was a dupe somewhere and that was the reason I posted it as a comment first. I only added as an answer when OP requested it without looking for a dupe (as that thought didn't cross my mind, so sorry for that). I have no problem removing the answer if OP can unaccept the answer. I will leave it up to OP to decide. – s7vr Mar 07 '17 at 14:45
  • hey @Veeram can you help with https://stackoverflow.com/questions/53708307/mongo-addtoset-an-array – kratos Dec 10 '18 at 16:08