1

I have the following structure:

{
    "title" : "Parent",
    "child" :
    {
        "title" : "Child"
    }
}

// I want to get only:

"child" :
{
    "title" : "Child"
}

I want to return only the child document. I'm trying to use the aggregation framework but i'm pretty new in Mongo and i'm lost here.

Any help? Thanks in advance.

  • Once you decide to "embded" data it is best to live with the fact that you have done this. If you actually do regularly need this data "separately", then you should in fact put the items in their own collection. So whilst you can do it with the aggregate method, it's really not great in terms of performance. You either want the data "together" or not. Where not, use another collection instead. – Neil Lunn Jul 01 '17 at 04:23

1 Answers1

0

db.collectionname.find({}, {child: 1, _id: 0})

The will return what you want, as long as there is only that one document in your collection. If you want to select the child document of a specific document by title, you can do this:

db.collectionname.find({title: "Parent"}, {child: 1, _id: 0})

Pat Needham
  • 5,698
  • 7
  • 43
  • 63