How to perform keyword query in mongodb?

Member

by adan , in category: MySQL , a month ago

How to perform keyword query in mongodb?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , a month ago

@adan 

To perform a keyword query in MongoDB, you can use the find() method along with a regular expression to search for a specific keyword in a collection. Here's an example:

1
db.collectionName.find({fieldName: {$regex: 'keyword', $options: 'i'}})


In this example:

  • db.collectionName: the name of the collection you want to query
  • fieldName: the name of the field in the collection you want to search for the keyword
  • 'keyword': the keyword you want to search for
  • $regex: operator used to perform a regex search
  • 'i': case-insensitive option to match the keyword regardless of case


You can also use other query operators in combination with the regex search to further refine your keyword query.