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

213
Views
Remove items from Mongo Aggregation if contained in an item array

Using MongoDB Node.js

I have an products collection:

Example:

{
    _id: '12',
    linkedProducts: ['1234']
}

If I had a product ID with an ID of 1234, and it is linked to '12', what would be the best way to lookup through all products that has 1234 as a linked product?

Any help would be greatly appreciated.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can first use the $in operator on the array field to find documents where the array contains the given id and then use $pull to remove this value from the array:

db.collection.update({
  linkedProducts: {
    $in: [
      "1234"
    ]
  }
},
{
  $pull: {
    linkedProducts: "1234"
  }
},
{
  multi: true
})

Here's a working example on mongoplayground: https://mongoplayground.net/p/nlBUWLdKERS

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!