1

So, for some reason the developers decided to store some information using dynamically named nested fields, and now the queries on these fields are (not surprisingly) sluggish.

The documents are like so:

{
    "_id:" : "2343244324",
    "field" : {
        "nested_9j3kj49j" : {
            // some other stuff here
        },
        "nested_j32kj43" : {
            // yet some other stuff here
        }
    },
    [other fields]  
}
***
{
    "_id:" : "57657656",
    "field" : {
        "nested_687fds6d7f6" : {
            // and yet some other stuff here
        }
    },
    [other fields]  
}

The mentioned slow queries are something like this:

db.Collection.find({ "field.nested_j32kj43" : { $exists: true} });

Is there anyway I could do this query in a more optimized way, or maybe create some sort of index to those nested fields, which vary in name and existence?

Christian Dechery
  • 876
  • 10
  • 31
  • Indexes are on the values or the data inside, rather than on keys. – hyades Nov 06 '17 at 18:59
  • 1
    One approach would be to add a new indexed array field (e.g. `fieldKeys`) to each doc with the existing keys as values (e.g. "nested_9j3kj49j") and then query against that field. – JohnnyHK Nov 06 '17 at 19:12

0 Answers0