Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create text index on sub-document field wildcard

Tags:

mongodb

I am experimenting with creating a text index in MongoDB for several fields in a sub-document. These fields are not the same from document to document. According to the documentation, I would create a normal text index like so:

db.collection.ensureIndex({
    subject: "text",
    content: "text"
});

In my case, I want the index on all fields in the fs.files collection at db.fs.files.metadata. I've tried this:

db.fs.files.ensureIndex({'metadata.$**': 'text'});

I don't believe this has worked, as searching with db.fs.files.runCommand('text'... returns zero results, and db.fs.files.stats() shows the index as a very small size (and I have ~35k documents in this collection).

How can I create a text index on field values of a subdocument where the keys are not known ahead of time?

like image 583
Brad Avatar asked Oct 02 '22 18:10

Brad


1 Answers

If you create a {'$**': 'text'} index on the parent document it will index the subdocument fields too. The docs say it only affects text so it will skip the file data but will include the name & contentType.

like image 144
tom Avatar answered Oct 07 '22 19:10

tom