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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:51:41+00:00 2026-06-05T19:51:41+00:00

In Java I have an object that looks like this : class MyDoc {

  • 0

In Java I have an object that looks like this :

class MyDoc {
     ObjectId docId;
     Map<String, String> someProps = new HashMap<String,String>(); 
}

which, when persisted to MongoDB produces the following document :

{
    "_id" : ObjectId("4fb538eb5e9e7b17b211d5d3"),
    "someProps" : {
        "4fda4993eb14ea4a4a149c04" : "PROCESSED",
        "4f56a5c4b6f621f092b00525" : "PROCESSED",
        "4fd95a2a0baaefd1837fe504" : "TODO"
    }
}

I need to query as follow.

DBObject queryObj =  
new BasicDBObject("someProps.4fda4993eb14ea4a4a149c04","PROCESSED");                        
DBObject explain =  
getCollection().find(queryObj).hint("props_indx").explain();

which should read find me the MyDoc documents that have a someProps with key “4fda4993eb14ea4a4a149c04” and value “Processed”

I have millions of MyDoc documents stored in the collection so I need efficient indexing on the keys of the someProps embedded object.

The keys of the map are not known in advance (they are dynamically generated, they are not a fixed set of keys) so I cannot create one index per someProps key. (at least I don’t think I can correct me if i’m wrong)

I tried to create the index directly on someProps but querying took ages.

How can Index on someProps Map keys ?
Do I need a different document structure ?

Improtant notes :

1 . There can only be ONE element of someProps with the same key. for example :

{
"_id" : ObjectId("4fb538eb5e9e7b17b211d5d3"),
    "someProps" : {
        "4fda4993eb14ea4a4a149c04" : "PROCESSED",
        "4f56a5c4b6f621f092b00525" : "PROCESSED",
        "4f56a5c4b6f621f092b00525" : "TODO"
    }
}

would be invalid because 4f56a5c4b6f621f092b00525 cannot be found two times in the Map (hence the use of a Map in the first place)

2 . I also need to efficiently update someProps, only changing the value (ex: changing “4fda4993eb14ea4a4a149c04” : “PROCESSED”, to “4fda4993eb14ea4a4a149c04” : “CANCELLED” )

What are my options ?

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-05T19:51:42+00:00Added an answer on June 5, 2026 at 7:51 pm

    I suggest expanding these properties to a documents of their own. So your example:

    {
        "_id" : ObjectId("4fb538eb5e9e7b17b211d5d3"),
        "someProps" : {
            "4fda4993eb14ea4a4a149c04" : "PROCESSED",
            "4f56a5c4b6f621f092b00525" : "PROCESSED",
            "4fd95a2a0baaefd1837fe504" : "TODO"
        }
    }
    

    becomes this

    {_id: {id1: ObjectId("4fb538eb5e9e7b17b211d5d3"), id2: "4fda4993eb14ea4a4a149c04"}, v: "PROCESSED"}
    {_id: {id1: ObjectId("4fb538eb5e9e7b17b211d5d3"), id2: "4f56a5c4b6f621f092b00525"}, v: "PROCESSED"}
    {_id: {id1: ObjectId("4fb538eb5e9e7b17b211d5d3"), id2: "4fd95a2a0baaefd1837fe504"}, v: "TODO"}
    

    Here id1 is id of your former parent entity (be it application or whatever) and id2 is property id.

    Uniqueness is enforced by properties of _id field. Atomic updates are trivial. Indexing is easy

    db.props.ensureIndex({'_id.id2': 1})
    

    The only disadvantage is some storage overhead.

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

Sidebar

Related Questions

I have code that looks like this: public class Polynomial { List<Term> term =
I have written a java annotation that looks like this: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) // can
I have a class that looks like this: public class Foobar { private float
I have a mocked method that looks like this: class NotMineClass { T Execute(Func
I have a Java Properties object that I load from an in-memory String ,
I'm working on a Java project where I have created a class that looks
I have a Json file that looks like this: [ { field:val }, ....
I have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <xs:msgdata xmlns:xs=http://www.myCompany.com
I have a hibernate object that gets detached and transferred to a thick java
I have a java.sql.ResultSet object containg data from a query that was run. How

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.