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

140
Views
Mongodb, query within key-pair values

Is there any way to query inside this struture by the object keys of roles?

   interface UsersModel{
         _id: string;
         email: string;
         roles: { [key: string]: RolesType[] };
    }

For now I get the data into users like:

roles: {
   $ne: null
}

and then

const finalResult = users.filter(u => Object.keys(u.roles).includes('objectKeyIwant'));

Can this be done on the mongodb side and avoid getting all the data then filter it?

Data examples: The user collection is like this:

{
    _id: '610baff85c071a2bd59dc84f',
    email: 'some@email.com',
    roles : {
        "shop1" : [ 
            "shop_stock_manager"
        ],
        "shop2" : [ 
            "shop_admin", 
            "shop_stock_manager"
        ]
    }
}

And I want to get all the users that have any role in shop2.

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

0

use this

db.collection.find({"roles.shop2":{$ne:[]}})
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!