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

266
Views
Unrecognized expression '$push' in MONGODB

This is how my sample QUESTION array is:

[
        {
            "subject": "app development",
            "topic": "dart",
            "subtopic": "flutter2",
            "questionType": "EASY",
            "questionTitle": "how angular works",
        },
        {
            "subject": "app development",
            "topic": "dart",
            "subtopic": "flutter",
            "questionType": "EASY",
            "questionTitle": "how angular works",
            
        },
        {
            "subject": "app development",
            "topic": "javascript",
            "subtopic": "react native",
            "questionType": "EASY",
            "questionTitle": "how angular works",
      
        },
    ]

I need to group the subject field into, and topic into an array.

This is how I want:

{
            "subject": "web development",
            "topics":[{ 
                topicName:"javascript", 
                subtopics:[
                        "react native,
                        "flutter"
                ]
               }]
}

I'm trying to do it this way but this error:

"Unrecognized expression '$push'" shows

{
          $group: {
            _id: "$subject",
            topics: {
              $addToSet: {
                title: "$topic",
                sub: {$push:"$subtopic"},
              },
            },
          },
        },

I need to get an array of grouped subjects and topics and subtopics.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The $group can not support nested operations $addToSet and $push at the same time, you need to divide both the operations into different $group stages,

  • $group by subject and topic and construct the unique array of subtopic using $addToSet
  • $group by only subject and construct the array of topics
  • $project to format and show required fields
[
  {
    $group: {
      _id: {
        subject: "$subject",
        topic: "$topic"
      },
      subtopic: { $addToSet: "$subtopic" }
    }
  },
  {
    $group: {
      _id: "$_id.subject",
      topics: {
        $push: {
          topic: "$_id.topic",
          sub: "$subtopic"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      subject: "$_id",
      topics: 1
    }
  }
]

Playground

about 4 years ago · Juan Pablo Isaza 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!