Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
Project field in embedded document within embedded array

I have the following query:

 cursor=self.postCol.aggregate([
      { "$graphLookup" : {
        "from": "pCol",
        "startWith": "$parent",
        "connectFromField": "parent",
        "connectToField": "_id",
        "as" : "parents"
        }
      },
      { "$project" : {
         "pValue": {
             "$cond": [
               { "$ne": [ "$pValue", [] ] },
               "$pValue",
               "$parents.0.pValue"
             ]
           }
         }
      }
    ])

so given then following records:

{"_id": 4, parent: "", "pValue": ["d"], fieldA: 9},
{"_id": 5, parent: 4, "pValue": [], fieldA:2},
{"_id": 6, parent: 4,"pValue": [], fieldA: 9}

I should get:

{"_id": 4, "pValue": ["d"]}
{"_id": 5, "pValue": ["d"]}
{"_id": 6, "pValue": ["d"]}

Instead I get:

{"_id": 4, "pValue": ["d"]}
{"_id": 5, "pValue": []}
{"_id": 6, "pValue": []}

The $parents.0.pValue isn't returning the right value for some reason which I don't understand. How can I select the field of pValue within the first element of the parents array?

I thought to use {"$arrayElemAt": ["$parents",0]} which will give me the embedded document but then how can I then get a field from that to be returned?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can replace the else condition with below using $let operator.

 {
    $let: {
        vars: {
            obj: {
                "$arrayElemAt": ["$parents", 0]
            }
        },
        in: "$$obj.pValue"
    }
 }
over 4 years ago · Santiago Trujillo Report

0

You can use the $switch operator and the $let operator to $project your field.

The $let variable operator let you assign a value to a variable which can be used in the "in" expression as shown in my other answer here.

With the $switch operator, we can perform very clean case-statement.

db.postCol.aggregate([
    { "$graphLookup": { 
        "from": "postCol",  
        "startWith": "$parent",  
        "connectFromField": "parent", 
        "connectToField": "_id",   
        "as" : "parents"           
    }}, 
    { "$project": { 
        "pValue": { 
            "$switch": { 
                "branches": [ 
                    { "case": { "$gt": [ { "$size": "$pValue"}, 0 ] },
                    "then": "$pValue" }
                ], 
                "default": { 
                    "$let": { 
                        "vars": { "p": { "$arrayElemAt": [ "$parents", 0 ] }}, 
                        "in": "$$p.pValue" 
                    }
                }
            }
        }
    }}
])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!