简体   繁体   中英

mongo $text {$search: "something"}

Trying to make searching in mongo using a part of the word that stored in the document

getCitiesWithQuery(reqQuery) {
    let query = {$text: {$search: reqQuery}};
    return db.collection('cities').find(query).toArray();
}

But I`ve got this error:

MongoError: text index required for $text query

How to fix it?

You need to create text index on that collection first. This example code below indexes every field that contains string data for each document in the collection

db.cities.createIndex(
   { "$**": "text" }
 )

By definition, $text looks for words only. If you're using MongoDB Atlas, you can make use of Atlas Search , which allows for partial searching (and fuzzy matching and other things).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM