DEV Community

Cover image for Your Complete Guide to MongoDB: Part Two (Advanced Query Filtering)
Abdelhakim mohamed
Abdelhakim mohamed

Posted on

Your Complete Guide to MongoDB: Part Two (Advanced Query Filtering)

Previous lessons


Table of contents


Download and import restaurants collection

  • Open mongodb compass
  • Connect to your local host

  • Open mongodb compass
    Image description

  • Create new database called shops

Image description

Image description

  • Click Add Data
  • Import JSON file
  • Select the downloaded JSON from my github repository

  • Open mongosh terminal


Use shops database.

 use shops
Enter fullscreen mode Exit fullscreen mode

Read Queries


$eq Stands for Equal

$eq is an operator used to match documents where the value of a field is equal to a specified value.

find documents with rating equal to 4.

db.restaurants.find( 
  {
    rating:{
      $eq: 4
    }
  }
)
//or you can just match directly without using $eq
db.restaurants.find( 
  {
    rating: 4
  }
)
Enter fullscreen mode Exit fullscreen mode

$elemMatch vs matching directly


db.restaurants.find(
  {
        "grades": {
          $elemMatch:{
           grade: "A",
            score:6
            grade: "A"
          }
        }
      }
)
Enter fullscreen mode Exit fullscreen mode
db.restaurants.find(
  {
        "grades.grade": "A"
        "grades.score": 6
   }
)
Enter fullscreen mode Exit fullscreen mode

matching directly like This means that the "grades" array may contain other elements besides the one with "grade" equal to "A" so it will return this document.

Image description


$elemMatch will make sure that the element inside grades array must have one element having score of 6 and grade of A so it will not return this document.

Image description


$ne

$ne stands for Not Equal.

find restaurants with grades not equal to "A".


db.restaurants.find(
{
      grades: {
        $elemMatch: {
          grade: {
            $ne: "A"
          }
        }
      }
    }
)
Enter fullscreen mode Exit fullscreen mode

$gt

$gt Stands For Greater Than.

find restaurants with rating greater than 3.


db.restaurants.find({ rating: { $gt: 3} })
Enter fullscreen mode Exit fullscreen mode

$gte

$gte Stands For Greater Than.

find restaurants with rating greater than or equal 4.


db.restaurants.find({ rating: { $gte: 4} })
Enter fullscreen mode Exit fullscreen mode

$lt

Stands for Less Than.

find restaurants with rating lower than 2.


db.restaurants.find({ shopRank: { $lt: 2} })
Enter fullscreen mode Exit fullscreen mode

$lte

Stands for Less Than Or Equal.

find restaurants with rating lower than or equal 5.

db.restaurants.find({ rating: { $lte: 5} })
Enter fullscreen mode Exit fullscreen mode

$in

Find restaurants in these three cuisine categories "Bakery", "Irish", "American" 1, 3, 5.

db.restaurants.find(
{
      cuisine: {
        $in: ["American", "Irish", "Bakery"]
      }
    }
)
Enter fullscreen mode Exit fullscreen mode

$nin

$nin stands for Not In.

Find restaurants not in these three shop ranks 1, 3, 5 .


db.restaurants.find(
{
      cuisine: {
        $nin: ["American", "Irish", "Bakery"]
      }
    }
)
Enter fullscreen mode Exit fullscreen mode

$and

$and is a logical operator used within queries to specify multiple conditions

$and returns all documents that meets all conditions specified

Find restaurants with with cuisine equal to "American" and rating higher than 3.


db.restaurants.find( 
  {
      $and: [
        { cuisine: "Irish" },
        { rating: { $gt: 1 } }
      ]
  }
)
Enter fullscreen mode Exit fullscreen mode

$or

$or is another logical operator used within queries to specify multiple conditions but unlike $and $or requires at least one condition to be met

Find restaurants with with cuisine equal to "American" or rating higher than 3.


db.restaurants.find( 
  {
      $or: [
        { cuisine: "Irish" },
        { rating: { $gt: 1 } }
      ]
  }
)
Enter fullscreen mode Exit fullscreen mode

$nor

$nor is the exact opposite or $or it finds documents that doesn't meet any of the conditions .

Find restaurants with with cuisine not equal to "American" or rating not higher than 3.


db.restaurants.find( 
  {
      $or: [
        { cuisine: "Irish" },
        { rating: { $gt: 1 } }
      ]
  }
)
Enter fullscreen mode Exit fullscreen mode

$not

$not is the exact opposite or $eq.

Find restaurants with with cuisine not equal to "American" or rating not higher than 3.


db.restaurants.find( 
  {
      cuisine: {
         $not: { $eq: "American" }
     }
  }
)
Enter fullscreen mode Exit fullscreen mode

$regex

$regex operator is used to perform regular expression-based pattern matching within queries. It allows you to search for documents where a specified field matches a regular expression pattern.

Find restaurants where the name starts with mor and case insensitive.


db.restaurants.find( 
  {
      title: {
        $regex: /mor/,
        $options: "i" // case insensitive option
     } 
  }
)
Enter fullscreen mode Exit fullscreen mode

$text and $search

$text and $search operator is used to perform a full-text search on text fields within a collection. It's specifically designed for searching text content efficiently.

Find restaurants where the name starts with morris.


db.restaurants.find( 
  {
      $text: { $search: "morris" }
  }
)
Enter fullscreen mode Exit fullscreen mode

$where

$where allows you to write javascript code directly into the query.

Find restaurants where the rating divided by two is still higher that 1.5 .


db.restaurants.find( 
  {
      $where: function () {
        return this.rating / 2  > 1.5;
      }
  }
)
Enter fullscreen mode Exit fullscreen mode



Image description

Next parts

Top comments (2)

Collapse
 
bytexbender profile image
b'

thx

Collapse
 
hakimmohamed profile image
Abdelhakim mohamed

Hope you liked it