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

171
Views
MongoDB - find documents with couple conditions

Requirement: COUNT all documents in the collection WHERE objects.objectType equal to 'group' AND (objects.objectType NOT equal to 'person' && relation equal to 'Exposed_to')

Expected: will return count of all documents WHERE objects.objectType equal to 'group' AND which does not contain any (objectType:person && realtion:Exposed_to) under 'objects' array.

An example of matched document:

"objects": [
    {
    "objectType": "organization",
    "relation": "Exposed_to"
    },
    {
    "objectType": "group",
    "relation": "Exposed_to"
    }
]

An example of document that shouldn't be counted:

"objects": [
    {
    "objectType": "person",
    "relation": "Exposed_to"
    },
    {
    "objectType": "group",
    "relation": "Exposed_to"
    }
]

i have tried the following query:

.count({
'objects.objectType': 'group',
'objects': {
    $elemMatch: {
    $and: [{'objectType' : {$ne: 'person'}, 'relation': 'Exposed_to'}]
    }
}})

but it seems to be not working correctly. i am not sure if i should use aggregation in this case- i also have less knowledge in this area.

i will be glad to get some help. thanks!

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

0

Is it working for you? https://mongoplayground.net/p/Q-dj7_O_AJR
This playground doesn't support count, so I replace by find

db.collection.find({
  $and: [
    {
      "objects.objectType": "group"
    },
    {
      objects: {
        $not: {
          "$elemMatch": {
            "objectType": "person",
            "relation": "Exposed_to"
          }
        }
      }
    }
  ]
})
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!