Text index on array of objects

Use case:

insert this document in a collection:

{
  title: "The adventure of Jane doe"
  authors: [{ name: "john doe" }]
}

creating a multiple fields text index :

db.mycollection.createIndex({title: “text”, “authors.name”: “text”})

querying:

db.mycollection.find({$text: {$search: “jan” }})

returns the document

querying:

db.mycollection.find({$text: {$search: “joh”}})

doesn’t return anything

querying:

db.mycollection.find({$text: {$search: “john”}})

returns the document

It seems that search text doesn’t search prefix on array of documents

If anyone has an explanation, i would be very grateful

Hi @Maxime_Radigue,

I actually surprised that the “jan” part worked and it might be just luck with stemming of words. Text indexes are designed to search words/phrases or delimited parts of words and not substrings.

https://docs.mongodb.com/manual/reference/operator/query/text/#search-field

The new Atlas search service have capabilities of text regex queries , however, legacy text indexes don’t.

Best
Pavel

1 Like