Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project nested document to root level without using $project

Using the aggregate pipeline, I am trying to project an embedded document to the root level WITHOUT projecting each field individually and without replacing root level.

For example, I want to project this collection to the root level:

[
    {
        _id: "1",
        city: "NY"
        user: [ {
            firstName: "John",
            lastname: "Peters",
            brothers: [
                { _id: "B1",
                  brotherFirstName: "Karl" }
            ]
         }, {
            firstName: "Raul",
            lastname: "Other",
            brothers: [
                { _id: "B2",
                  brotherFirstName: "May" }
            ]
         }, {
            firstName: "Paul",
            lastname: "Ray",
            brothers: [
                { _id: "B3",
                  brotherFirstName: "Bryan" }
            ]
         }        
    },
    {
        _id: "2",
        city: "NY"
        user: [ {
            firstName: "Joe",
            lastname: "Anders",
            brothers: [
                { _id: "B4",
                  brotherFirstName: "Carla" }
            ]
         }, {
            firstName: "Zoy",
            lastname: "Bat",
            brothers: [
                { _id: "B5",
                  brotherFirstName: "Stuart" }
            ]
         }, {
            firstName: "Ana",
            lastname: "Lily",
            brothers: [
                { _id: "B6",
                  brotherFirstName: "Carter" }
            ]
         }        
    }
]

This is what I am looking for: for each document in subdocument array I want to project it to root level, so for each "user" I want to project it to "new document" and for each "brother" I want to project it to "new document"

[{
  _id: "1",
  city: "NY"
  firstName: "John",
  lastname: "Peters",
  brotherFirstName: "Karl" 
}, { 
  _id: "1",
  city: "NY"
  firstName: "Raul",
  lastname: "Other",
  brotherFirstName: "May" 
}, {
  _id: "1",
  city: "NY"
  firstName: "Paul",
  lastname: "Ray",
  brotherFirstName: "Bryan" 
}, {
  _id: "2",
  city: "NY"
  firstName: "Joe",
  lastname: "Anders",
  brotherFirstName: "Carla"
 }, {
  _id: "2",
  city: "NY"
  firstName: "Zoy",
  lastname: "Bat",
  brotherFirstName: "Stuart"
 }, {
  _id: "2",
  city: "NY"
  firstName: "Ana",
  lastname: "Lily",
  brotherFirstName: "Carter"
 }        
]

I tried aggregate $unwind e $replaceRoot, but I cant "replace root" because I nedd theese fields

db.getCollection('myColl').aggregate([  { $unwind : "$users" },  {$replaceRoot: { newRoot: "$users" }}, {$unwind : "$brothers" } ])

EDIT

Running @Anthony Winzlet query I had this output:

[
  {
    "_id": "B1",
    "brotherFirstName": "Karl",
    "city": "NY",
    "firstName": "John",
    "lastname": "Peters"
  },
  {
    "_id": "B2",
    "brotherFirstName": "May",
    "city": "NY",
    "firstName": "Raul",
    "lastname": "Other"
  },
  {
    "_id": "B3",
    "brotherFirstName": "Bryan",
    "city": "NY",
    "firstName": "Paul",
    "lastname": "Ray"
  },
  {
    "_id": "B4",
    "brotherFirstName": "Carla",
    "city": "NY",
    "firstName": "Joe",
    "lastname": "Anders"
  },
  {
    "_id": "B5",
    "brotherFirstName": "Stuart",
    "city": "NY",
    "firstName": "Zoy",
    "lastname": "Bat"
  },
  {
    "_id": "B6",
    "brotherFirstName": "Carter",
    "city": "NY",
    "firstName": "Ana",
    "lastname": "Lily"
  }
]

The field _id was overwritten by brother's _id field. I need project _id from root

like image 470
Guilherme Ferreira Avatar asked Dec 06 '18 16:12

Guilherme Ferreira


People also ask

What is the use of$ project in mongodb?

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.

What is$ replaceRoot in mongodb?

Definition. Replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field. You can promote an existing embedded document to the top level, or create a new document for promotion (see example).

What is $$ root in Mongodb?

The $$ROOT variable contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.

What is project nested?

Project Nested is a NES emulator running on the raw power of the SNES. Inspired by the SNES originally designed to be backward compatible and wanting to code a JIT compiler.

How is the hierarchy of a nested project built?

The process is recursive and the hierarchy is built from the top down. When a nested project is closed because the user closed the solution or the specific project itself, the other method on IVsParentProject, CloseChildren, is called.

How do I delegate a project to a nested project?

The GetProperty method is then called for the parent ItemID that by convention you delegate in to the nested project. The GetProperty retrieves the properties of the node that nests a project that you want to delegate in when it is called on the parent.

What is the getProperty in Node JS?

The GetProperty retrieves the properties of the node that nests a project that you want to delegate in when it is called on the parent. Because parent and child projects are instantiated programmatically, you can set properties for nested projects at this point.


1 Answers

You need to use $mergeObjects with the $$ROOT document. First with the user and second with the brother and last you have to use $project to remove the user and brother objects

db.collection.aggregate([
  { "$unwind": "$user" },
  { "$replaceRoot": {
    "newRoot": { "$mergeObjects": ["$$ROOT", "$user"] }
  }},
  { "$unwind": "$brothers" },
  { "$replaceRoot": {
    "newRoot": { "$mergeObjects": ["$brothers", "$$ROOT"] }
  }},
  { "$project": { "brothers": 0, "user": 0 }}
])

Output

[
  {
    "_id": "1",
    "brotherFirstName": "Karl",
    "city": "NY",
    "firstName": "John",
    "lastname": "Peters"
  },
  {
    "_id": "1",
    "brotherFirstName": "May",
    "city": "NY",
    "firstName": "Raul",
    "lastname": "Other"
  },
  {
    "_id": "1",
    "brotherFirstName": "Bryan",
    "city": "NY",
    "firstName": "Paul",
    "lastname": "Ray"
  },
  {
    "_id": "2",
    "brotherFirstName": "Carla",
    "city": "NY",
    "firstName": "Joe",
    "lastname": "Anders"
  },
  {
    "_id": "2",
    "brotherFirstName": "Stuart",
    "city": "NY",
    "firstName": "Zoy",
    "lastname": "Bat"
  },
  {
    "_id": "2",
    "brotherFirstName": "Carter",
    "city": "NY",
    "firstName": "Ana",
    "lastname": "Lily"
  }
]
like image 161
Ashh Avatar answered Oct 15 '22 23:10

Ashh