0

I have an collections. Let's take one element from collection:

{
    "messages" : {
        "_id" : ObjectId("5503044be4b0e3d1aed29d15"),
        "body" : "Hello YOU!",
        "subject" : "sth"
     }
}

and now the tricky part. How to get elements from collection without wrapper. Like below:

{    
  "_id" : ObjectId("5503044be4b0e3d1aed29d15"),
  "body" : "Hello YOU!",
  "subject" : "sth"
}
2
  • @Michael The answer wasn't compleate. But I just voted up. Commented Mar 20, 2015 at 21:04
  • What else is missing? Commented Mar 20, 2015 at 21:07

1 Answer 1

3

It is simple you can use $project to add or reset a field within the document.

db.collection.aggregate([
    { 
        "$project": {
            "_id": "$messages._id",
            "body": "$messages.body", 
            "subject": "$messages.subject"
         } 
     }
])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.