Suppose I have a collection of companies that contains fields such as phone, name, companyName, email. I want the admin to search on the basis of either phone, name or email.
You will find the document for find() at https://docs.mongodb.com/manual/reference/method/db.collection.find/
I would also recommend that you take the course M001 from https://university.mongodb.com/
Adding to the question, I know the basic find query, I should have mentioned in question that I want to make search on the basis of one of the fields at a time with regular expressions kind of thing. Like if Ama is typed then results starting with Ama would appear in email field, or like if 9465 is typed then companies whose phoneNo starts from 9465 would be returned.
In this case what you want is
or
Hi, for multi field RegExp search do we need a single compount index for both fielts together or we need separate indexes for both fields? what if data is huge like 500 million documents?
Thanks
If the query is multifield, a multifield index would help.
If you use Atlas, perhaps Atlas Search will help more than making a regex query.
For a query like this:
{
Name: { ‘$regex’: ‘john smith’, ‘$options’: ‘i’ },
City: { ‘$regex’: ‘new york’, ‘$options’: ‘i’ }
}
what kind of index/indexes do i need on the mongodb?
Things to consider:
both Name and City fields are “String” and can be more than john smith like a middle name and addresses are long too not just new york.
data is huge something like 500 million documents.
Questions.
Do i need compound index of both fields together with asc like Name_1_City_1
or
Do i need separate indexes (if yes then which type i mean asc, text etc) for both fields?
or any other type?
You would create the following index
db.<collection>.createIndex(
{
Name: "text",
City: "text",
}
)
You can only create one text index per collection, but that index can span multiple fields.
Documentation: https://www.mongodb.com/pt-br/docs/manual/core/indexes/index-types/index-text/