Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB : how to index the keys of a Map

Tags:

java

mongodb

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.

like image 642
azpublic Avatar asked Jun 17 '12 20:06

azpublic


People also ask

How do I index a field in MongoDB?

MongoDB provides complete support for indexes on any field in a collection of documents. By default, all collections have an index on the _id field, and applications and users may add additional indexes to support important queries and operations. This document describes ascending/descending indexes on a single field.

What is index key MongoDB?

MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays.

Is it possible to index text with MongoDB?

MongoDB provides text indexes to support text search queries on string content. Text indexes can include any field whose value is a string or an array of string elements. A collection can only have one text search index, but that index can cover multiple fields.

How do I see indexes in MongoDB?

You can find all the available indexes in a MongoDB collection by using the getIndexes method. This will return all the indexes in a specific collection. Result: The output contains the default _id index and the user-created index student name index.


1 Answers

If you want to keep your properties embedded, you could also use the dynamic attributes pattern as proposed by Kyle Banke in "MongoDB in Action". So instead of putting the props in their own collection, you modify your mydocs collection to look like this:

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

And then index on the embedded document keys:

db.mydoc.ensureIndex({'someProps.k' :1}, {'someProps.v' :1})

This is very close to what Sergio suggested, but your data will still be one document in a single collection.

like image 58
c089 Avatar answered Sep 28 '22 19:09

c089