Support for multiple filters and wildcard search?

Does Mongo DB have support for filtering the DB based on multiple filters at the same time?

Also does it have support for wildcard search, for example - ‘Like’ keyword support or anything synonymous?

Edit: Just noticed this post is in the chapter 1 forum and the lecture I mention is in chapter 2.

You can filter for documents which meet all the conditions in a query, this is covered in the lecture “Reading Documents: Scalar Fields”

{mpaaRating: "PG-13", year: 2009}

In SQL server this would be

where mpaaRating='PG-13' and year=2009

Chapter 2 doesn’t cover filtering for documents which meet any of the conditions in a query, but there was a mention that this is covered later in the course. However, you can use the $or operator and pass it an array of conditions:

{$or: [{mpaaRating:"PG-13"}, {year: 2009}]}

In SQL server this would be

where mpaaRating='PG-13' or year=2009

Reference: https://docs.mongodb.com/manual/reference/operator/query/or/#op._S_or

You can also use the $in operator to test a field against a list of values:

{year: {$in: [2009, 2012, 2015]}}

Equivalent to

where year in (2009, 2012, 2015)

Reference: https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in

The closest I can find to using wildcards is regular expressions:

{plot: /apple/}

Which is similar to (but possibly not identical to)

where plot like '%apple%'

Reference: sql - How to query MongoDB with "like" - Stack Overflow

And if you’re not familiar with regular expressions then this site is an absolute godsend: https://www.regular-expressions.info/

Hope this is useful.

1 Like

Thanks Simon.
I will check that in chapter 2.