For example if my keyword is "all", I would like MongoDb to return all the documents in the given field containing the words "all" like "edgar allan poe" or "whitney hall". How do I do that in MongoDb? I've been stuck here for days so any help would be greatly appreciated :)
Create a Wildcard Index on All Fields With this wildcard index, MongoDB indexes all fields for each document in the collection. If a given field is a nested document or array, the wildcard index recurses into the document/array and stores the value for all fields in the document/array.
A wildcard character is used to search for a partial value in a field when you do not know the entire value. You place the wildcard where the unknown characters occur. You can use more than one wildcard character in a single search.
You can use the asterisk (*) anywhere in a character string. wh* finds what, white, and why, but not awhile or watch. Matches a single alphabet in a specific position.
Match a character within a pattern Open your query in Design view. In the Criteria cell of the field you want to use, type the operator Like in front of your criteria. Replace one or more characters in the criteria with a wildcard character. For example, Like R?
By using a regular expression. MongoDb has a $regex operator.
db.authors.find({name: {$regex: 'all', $options: 'i'}});
Regex operations cannot use indexes, so find() operations won't be efficient. An index can only be used for prefix(starts with) and case-sensitive matches like this query:
db.authors.find({name: {$regex: '^a'});
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