Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:53:02+00:00 2026-06-14T09:53:02+00:00

I have a MongoDB document structured like this: { user_id: 7387, first_name: Daniel, roles:

  • 0

I have a MongoDB document structured like this:

{
  "user_id": 7387,
  "first_name": "Daniel",
  "roles": [
    {
      "role_id": "509c20144fd846549e8d42ef",
      "role_name": "Writer",
      "is_lead": 0
    },
    {
      "role_id": "509c20344fd846549e8d42f0",
      "role_name": "Editor",
      "is_lead": 1
    }
  ]
},
{
  "user_id": 15735,
  "first_name": "Tom",
  "roles": [
    {
      "role_id": "509c203f4fd846549e8d42f1",
      "role_name": "Admin",
      "is_lead": 0
    },
    {
      "role_id": "509c20144fd846549e8d42ef",
      "role_name": "Writer",
      "is_lead": 1
    },
    {
      "role_id": "509c20344fd846549e8d42f0",
      "role_name": "Editor",
      "is_lead": 0
    }
  ]
}

How do I find all users who have roles.role_name as Writer and roles.is_lead as 1 (all writers who are also a lead)?

I tried:

db.users.find({$and:[{"roles.role_name":"Writer","roles.is_lead":1}]});

In the above example, it should only return “Tom”. But this returns both “Tom” and “Daniel” (I guess it picks {roles.is_lead:1} for the Editor role of “Daniel”. How do I fix this query?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:53:03+00:00Added an answer on June 14, 2026 at 9:53 am

    What you are looking for is $elemMatch to make sure your search matches a specific element and not a mixture of any elements within the same document.

    db.test.find({roles: {$elemMatch: {role_name:"Writer", is_lead:1}}})
    

    Your original query was also the same as just doing a default basic search, since that will be an AND as well. The problem is that the AND was applying to any part of the document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a document in mongodb that is structured similar to this: { _id:
I have a document structure in MongoDB like this: User |---> Posts |----> Comments
I have this document stored in mongodb document: { _id:ObjectId(4eb7642ba899edcc31000001) hash:abcd123 value:some_text_here } I
If I have a document in mongodb with this structure: { _id: user1, loc:
I have nodejs application with mongodb(mongoose). like this: var mongoose = require('mongoose'); var express
i have a structure of document in mongodb as below { _id: some_id, key1:
The limitation of one document in MongoDB is 4Mo. I have some documents really
I have 10000 documents in one MongoDB collection. I'd like to update all the
In MongoDB I have a collection of documents called 'clients', where each document is
On this post MongoDB finding nested elements , author claims that the mongodb document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.