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

198
Views
MongoDB query using intersection of 2 lists

I'm working on a project where we use mongodb 4.4 as our database. The problem is I have to make a query that is too complicated using mongo query language.

That's an exemple of the data in the database:

{
    "neededQuantity": 100,
    "auth": {
        "groups": ["group-1"]
    },
    "createTime": "2022-02-03T14:17:25.809704600Z",
},
{
    "neededQuantity": 120,
    "auth": {
        "groups": ["group-3"]
    },
    "createTime": "2022-02-02T14:17:25.809704600Z",
},
{
    "neededQuantity": 150,
    "auth": {
        "groups": ["group-2","group-1"]
    },
    "createTime": "2022-02-01T14:17:25.809704600Z",
}

The query has to find the documents that have at least 1 element of the list auth.groups in common with an another list that is given as input.

If the input list is ["group-1"], the query result should be:

{
    "neededQuantity": 100,
    "auth": {
        "groups": ["group-1"]
    },
    "createTime": "2022-02-03T14:17:25.809704600Z",
},
{
    "neededQuantity": 150,
    "auth": {
        "groups": ["group-2","group-1"]
    },
    "createTime": "2022-02-01T14:17:25.809704600Z",
}

How can I do it?

Thank you very much in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

you can do it with $in

db.collection.find({
    "auth.groups": {$in: ["group-1"]}
})

playground

more detail about $in here

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!