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 8390373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:58:03+00:00 2026-06-09T18:58:03+00:00

tricky mongodb querying problem: I have a collection ‘Accounts’ with a bunch of documents

  • 0

tricky mongodb querying problem: I have a collection ‘Accounts’ with a bunch of documents that (simplified and with actual data swapped for non-real-world values) look like this:

{"_id": "<Mongo Binary Data>",
 "Roles": {
     "D7753879C7020F8ECF947122FA211413": {
       "_id": "<Mongo Binary Data>",
       "OrgName": "ACME",
       "Rolename": "Coyote Liaison",
    },

     "CFA7722E6799170706E4C5FFF3F01E63": {
       "_id": "<Mongo Binary Data>",
       "OrgName": "ACME",      
       "Rolename": "MembershipAdmin",
    },

     "C7020F8ECF947122FAGIGHFVFF3F7753": {
       "_id": "<Mongo Binary Data>",
       "OrgName": "Initech",       
       "Rolename": "MembershipAdmin",
    }   
  } 
}

The keys in the Roles array are a combination of a role id and an org id which are then hashed, which makes it very quick to query (once an Account object has been loaded up from MongoDB and into C#) for if an Account has a given role for a given org, ie, is a user a MembershipAdmin for Initech.

Now I want to query for users who have a role for ANY org, which in pseudo-SQL could be expressed as ‘SELECT all accounts which have at least one role object where Rolename = THISROLENAME’. Ie, get all users which are MembershipAdmins.

I have tried this:

{
  Roles.Rolename: "MembershipAdmin"
}

and this

{
  Roles: {"Rolename": "MembershipAdmin"}
}

and this

{
  Roles: {"$elemMatch": {"Rolename": "MembershipAdmin"}}
}

…to no avail and have seen several answers, etc, saying that the only way out of this is to push the associative array key down into the subobjects, which I don’t want to do as it makes the primary function of this data structure (checking if an account has a given role for a given organisation) very quick indeed. The use case I describe above doesn’t have to be mega-fast as it is part of an admin user’s responsibility so I’m happy to make them wait a few moments – so queries with excessive recursion, etc, are OK in this case.

Does anyone have any ideas how to make this work without refactoring the data structure? At my wit’s end with this.

Many thanks,

G

[EDIT: the structure above is not queryable, see the accepted answer for an erudite but quick explanation as to why not and what you should properly do to fix it. If you’re OK with a hacky workaround, though, you can store a copy of the data in a BsonArray alongside it and query against that using $elemMatch]

  • 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-09T18:58:04+00:00Added an answer on June 9, 2026 at 6:58 pm

    There is no way to do with you want with that schema, nor is the schema very practical to begin with. You should change your schema to this :

    {"_id": "<Mongo Binary Data>",
     "Roles": [
        {
           "hash": "D7753879C7020F8ECF947122FA211413",
           "_id": "<Mongo Binary Data>",
           "OrgName": "ACME",
           "Rolename": "Coyote Liaison",
        },
    
        {
           "hash": "CFA7722E6799170706E4C5FFF3F01E63",
           "_id": "<Mongo Binary Data>",
           "OrgName": "ACME",      
           "Rolename": "MembershipAdmin",
        },
    
        {
           "hash": "C7020F8ECF947122FAGIGHFVFF3F7753",
           "_id": "<Mongo Binary Data>",
           "OrgName": "Initech",       
           "Rolename": "MembershipAdmin",
        }   
      ] 
    }
    

    The reason why using nonstable values as document field names is so unpractical is exactly because it complicates most querying. The only upside is that some queries might be a little faster but given the fact that it also causes issues for indexing it’s almost always a bad idea.

    I would strongly suggest to change your schema to the above rather than look for solutions that allow your current schema.

    EDIT: As mentioned in the discussion below it is technically possible to create the needed query using the $where operator. If you cannot do it any other way it means you have a schema smell and a potential scaling and performance bottleneck that will be diabolically hard to fix when your software goes live. Don’t use $where. Ever.

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

Sidebar

Related Questions

I have a tricky problem that I've been messing about with for a few
I have a tricky problem: there is a file (version.txt) that contains two lines
I have a very tricky problem going on with content inside a textarea on
I have the following tricky problem: I have implemented a (rather complicated) class which
I have a tricky problem with SSRS. One of my users had his AD
How do I save a data to MongoDB? I have a about 1GB size
I have one very tricky issue that I am not able to sort out
I have a tricky question. As everyone knows, Apple clearly says that ONE UIViewController
Here's a tricky one. I have data as follows: User ID Name Skill Sets
That's tricky. I have a small button over a very big one. When the

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.