Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying in MongoDB Compass

Tags:

mongodb

Just a quick question I hope someone can answer for me.

I have a collection in MongoDB containing 84 thousand documents. The data looks something like this:

Data

There are several thousand documents containing the word "BOND" as the category name such as this:

More Data

Even More Data

And many thousands more...

Currently in MongoDB Compass I am using the following query:

{ "Category" : "BOND" } 

But of course this just returns 1 document that where the Category is BOND.

Can anybody tell me how I can query to find all documents where the field name "Category" contains the word "BOND" within it?

Many thanks, G

like image 261
GBT97 Avatar asked Sep 08 '17 09:09

GBT97


People also ask

Can I write query in MongoDB compass?

You can type MongoDB filter documents into the query bar to display only documents which match the specified criteria. To learn more about querying documents, see Query Documents in the MongoDB manual.

How do I query data in MongoDB?

The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.

What is query operation in MongoDB?

The query operators enhance the functionality of MongoDB by allowing developers to create complex queries to interact with data sets that match their applications. MongoDB offers the following query operator types: Comparison. Logical.


1 Answers

You should use regexp for this, i.e.

{ "Category" : /^BOND.*/ } 

for categories begins with BOND, or

{ "Category" : /.*BOND.*/ } 

for categories contains BOND within

like image 89
Alexey Sviridov Avatar answered Sep 29 '22 16:09

Alexey Sviridov