what is wildcard in mongoDB ? Give me details of wildcard index or search with example…
Hi @Ajay_Moladiya,
Welcome to the MongoDB Community forums
The wildcard operator enables queries that use special characters in the search string that can match any character.
Character | Description |
---|---|
? |
Matches any single character. |
* |
Matches 0 or more characters. |
\ |
Escape character. |
To learn more about wildcard operators, please refer to this link
Wildcard indexing is an index that can filter and automatically matches any field, sub-document, or array in a collection and then index those matches.
Consider an application that captures product data under the productMetadata
field and supports querying against that data:
{ "productMetadata": { "category": "electronics", "brand": "Apple" } }
{ "productMetadata": { "category": "clothing", "brand": "Nike" } }
{ "productMetadata": { "category" : "electronics", "manufacturer": "Sony" } }
{ "productMetadata": "out of stock" }
Now, the user wants to create indexes to support queries on any subfield of productMetadata
. A wildcard index on productMetadata
can support single-field queries on productMetadata
, productMetadata.category
, productMetadata.brand
, and productMetadata.manufacturer
:
db.products.createIndex( { "productMetadata.$**" : 1 } )
The index can support the following queries:
db.products.find({ "productMetadata.category" : "electronics" })
db.products.find({ "productMetadata.brand" : "Nike" })
db.products.find({ "productMetadata.manufacturer" : "Sony" })
db.products.find({ "productMetadata" : "out of stock" })
In all of these queries, the index on productMetadata.$**
would be used to speed up the query. The $**
wildcard specifier matches all subfields of the productMetadata
field, so the index can be used to satisfy any query that involves productMetadata
.
To learn more, please refer to this link:
I hope it helps. Let us know if you have any further questions.
Regards,
Kushagra
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.