What is the alternate option for text index on object field?

Document structure like this:

{
   firstObject: {
     firstField: "Some value",
     secondField: "Some value",
   },
   secondObject: {
     firstField: "Some value",
     secondField: "Some value",
   }
}

I want to create text index on both objects firstObject and secondObject because both object having dynamic fields, I have tried this:

db.collection.createIndex({ firstObject: "text", secondObject: "text" });

But unfortunately this is not working, as per documentation mention, it work on string and array of string:

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.

Is there any other option to work my scenario? I don’t want to mention each and every object’s field manually in create index because all keys are dynamic.

I think that with https://www.mongodb.com/blog/post/building-with-patterns-the-attribute-pattern you could have an index on k that will support your queries.

1 Like

Thank you, yah that was the last option i thought after my post,

I figured that 2 options:

  1. Convert that object in to array in k and v format
  2. Wildcard text index

I will work around which is efficient and better for my database

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.