TL; DR
Manière correcte de le faire en mongo
N'utilisez pas RegExp
Devenez naturel et utilisez l'indexation intégrée de Mongodb, recherchez
Étape 1 :
db.articles.insert(
[
{ _id: 1, subject: "coffee", author: "xyz", views: 50 },
{ _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
{ _id: 3, subject: "Baking a cake", author: "abc", views: 90 },
{ _id: 4, subject: "baking", author: "xyz", views: 100 },
{ _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
{ _id: 6, subject: "Сырники", author: "jkl", views: 80 },
{ _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
{ _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
]
)
Étape 2 :
Besoin de créer un index sur le champ TEXTE que vous souhaitez rechercher, sans requête d'indexation sera extrêmement lent
db.articles.createIndex( { subject: "text" } )
étape 3 :
db.articles.find( { $text: { $search: "coffee",$caseSensitive :true } } ) //FOR SENSITIVITY
db.articles.find( { $text: { $search: "coffee",$caseSensitive :false } } ) //FOR INSENSITIVITY
$caseSensitive: false
. Voir: docs.mongodb.org/manual/reference/operator/query/text/…