Search code examples
mongodb

Querying in MongoDB Compass


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


Solution

  • You should use regexp for this, i.e.

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

    for categories begins with BOND, or

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

    for categories contains BOND within