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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With