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

109
Views
How to exclude array of ids from query

I have an array of _ids and I want to exclude them from the query:

console.log('relatedCards', relatedCards);
const motherCards = await db.MotherCard.find({ "_id": { $ne : relatedCards } });

But it returns this error:

no

Why this is happening and how to fix this ?

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

0

$ ne is usually used for a value. But if you want to exclude multiple items in array, you can use $ nin

For example I have this model:

{
  "_id": "5a934e000102030405000000",
  "key": 1
},
{
  "_id": "5a934e000102030405000001",
  "key": 2
},
{
  "_id": "5a934e000102030405000002",
  "key": 3
},
{
  "_id": "5a934e000102030405000003",
  "key": 4
},
{
  "_id": "5a934e000102030405000004",
  "key": 5
}

And I want exclude key:3 and key5:

db.collection.find({
key: {
  "$nin": [
    3,
    5
  ]
}
})

Now this query return a correct result:

[
  {
    "_id": "5a934e000102030405000000",
    "key": 1
  },
  {
    "_id": "5a934e000102030405000001",
    "key": 2
  },
  {
    "_id": "5a934e000102030405000003",
    "key": 4
  }
]

I hope this solution helps you.

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!