Your Complete Guide to MongoDB: Part Two (Advanced Query Filtering)
Tags: database, mongodb, nosql, backend
Published: 2024-05-05 10:37:55

<h2>Previous lessons</h2>
<ul>
  <li><a href="https://dev.to/hakimmohamed/your-complete-guide-to-mongodb-part-one-setup-and-basics-30n3">Part one</a></li>
</li>
</ul>

<hr> 

<h2>Table of contents</h2>
<ul>
  <li><a href="#install">Download and import restaurants data set</a></li>
  <li><a href="#advancedreadqueries">Read queries</a></li>
</ul>

<hr>

<h2 id="install">Download and import restaurants collection</h2>
  

- Open mongodb compass 
- Connect to your local host

- Open mongodb compass
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l2o2483eozact11rdkj.png)

- Create new database called shops

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vriz9o3tnlaxel14qhal.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41evmijkry9cxwwpnpfp.png)

- Click Add Data
- Import JSON file
- Select the downloaded JSON from my github <a href ="https://github.com/HakimMohamed/Your-Complete-Guide-to-MongoDB-Article">repository</a>

- Open mongosh terminal

<hr>
<blockquote>
  <h2>Use shops database.</h2>
</blockquote>

```bash
 use shops
```

<hr>

 <h2 id="advancedreadqueries">Read Queries</h2>
<blockquote>
  <ul>
    <li><strong>Comparison:</strong>
      <ul>
        <li><a href="#$eq"> $eq: <a/></li>
        <li><a href="#$elemMatch"> $elemMatch: <a/></li>
        <li><a href="#$ne"> $ne: <a/></li>
        <li><a href="#$gt"> $gt: <a/></li>
        <li><a href="#$gte"> $gte: <a/></li>
        <li><a href="#$lt"> $lt: <a/></li>
        <li><a href="#$lte"> $lte: <a/></li>
        <li><a href="#$in"> $in: <a/></li>
        <li><a href="#$nin"> $nin: <a/></li>

      </ul>
    </li>
    <li><strong>Logical:</strong>
      <ul>
        <li><a href="#$and"> $and: <a/></li>
        <li><a href="#$or"> $or: <a/></li>
        <li><a href="#$nor"> $nor: <a/></li>
        <li><a href="#$not"> $not: <a/></li>
      </ul>
    </li>

    <li><strong>Evaluation:</strong>
      <ul>
        <li><a href="#$regex"> $regex: <a/></li>
        <li><a href="#$text"> $text: <a/></li>
        <li><a href="#$where"> $where: <a/></li>

      </ul>
    </li>

  </ul>
</blockquote>
<hr>


 <h2 id="$eq">$eq Stands for Equal</h2>
<blockquote>
  <p>$eq is an operator used to match documents where the value of a field is equal to a specified value.</p>
  <p>find documents with rating equal to 4.</p>

</blockquote>

```bash
db.restaurants.find( 
  {
    rating:{
      $eq: 4
    }
  }
)
//or you can just match directly without using $eq
db.restaurants.find( 
  {
    rating: 4
  }
)
```
<hr>

 <h2 id="$elemMatch">$elemMatch vs matching directly</h2>
 
```bash
db.restaurants.find(
  {
        "grades": {
          $elemMatch:{
           grade: "A",
            score:6
            grade: "A"
          }
        }
      }
)
```

```bash
db.restaurants.find(
  {
        "grades.grade": "A"
        "grades.score": 6
   }
)
```

<blockquote>
  <p>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.</p>
</blockquote>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cklrdg1ep5nig86jqprb.png)

<hr>


<blockquote>
  <p>$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.</p>
</blockquote>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0o848wtq748a2iv56wk.png)





<hr>

 <h2 id="$ne">$ne</h2>

<blockquote>
  <p>$ne stands for Not Equal.</p>
  <p>find restaurants with grades not equal to "A".</p>
</blockquote>

```bash
db.restaurants.find(
{
      grades: {
        $elemMatch: {
          grade: {
            $ne: "A"
          }
        }
      }
    }
)
```

<hr>

<h2 id="$gt">$gt</h2>

<blockquote>
  <p>$gt Stands For Greater Than.</p>
  <p>find restaurants with rating greater than 3.</p>
</blockquote>

```bash
db.restaurants.find({ rating: { $gt: 3} })
```

<hr>

<h2 id="$gte">$gte</h2>

<blockquote>
  <p>$gte Stands For Greater Than.</p>
  <p>find restaurants with rating greater than or equal 4.</p>
</blockquote>

```bash
db.restaurants.find({ rating: { $gte: 4} })
```

<hr>

<h2 id="$lt">$lt</h2>

<blockquote>
  <p>Stands for Less Than.</p>

  <p>find restaurants with rating lower than 2.</p>
</blockquote>

```bash
db.restaurants.find({ shopRank: { $lt: 2} })
```

<hr>

<h2 id="$lte">$lte</h2>

<blockquote>
  <p>Stands for Less Than Or Equal.</p>  
<p>find restaurants with rating lower than or equal 5.</p>
</blockquote>

```bash
db.restaurants.find({ rating: { $lte: 5} })
```

<hr>

<h2 id="$in">$in</h2>

<blockquote>
  <p>Find restaurants in these three cuisine categories "Bakery", "Irish", "American" 1, 3, 5.</p>
</blockquote>

```bash
db.restaurants.find(
{
      cuisine: {
        $in: ["American", "Irish", "Bakery"]
      }
    }
)
```

<hr>

<h2 id="$nin">$nin</h2>

<blockquote>
  <p>$nin stands for Not In.</p>
  <p>Find restaurants not in these three shop ranks 1, 3, 5 .</p>
</blockquote>

```bash
db.restaurants.find(
{
      cuisine: {
        $nin: ["American", "Irish", "Bakery"]
      }
    }
)
```

<hr>

<h2 id="$and">$and</h2>

<blockquote>
 <p>$and is a logical operator used within queries to specify multiple conditions</p>

<p>$and returns all documents that meets all conditions specified</p>

  <p>Find restaurants with with cuisine equal to "American" and rating higher than 3.</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      $and: [
        { cuisine: "Irish" },
        { rating: { $gt: 1 } }
      ]
  }
)
```

<hr>

<h2 id="$or">$or</h2>

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

  <p>Find restaurants with with cuisine equal to "American" or rating higher than 3.</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      $or: [
        { cuisine: "Irish" },
        { rating: { $gt: 1 } }
      ]
  }
)
```

<hr>

<h2 id="$nor">$nor</h2>

<blockquote>
 <p>$nor is the exact opposite or $or it finds documents that doesn't meet any of the conditions .</p>

  <p>Find restaurants with with cuisine not equal to "American" or rating not higher than 3.</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      $or: [
        { cuisine: "Irish" },
        { rating: { $gt: 1 } }
      ]
  }
)
```


<hr>

<h2 id="$not">$not</h2>

<blockquote>
 <p>$not is the exact opposite or $eq.</p>

  <p>Find restaurants with with cuisine not equal to "American" or rating not higher than 3.</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      cuisine: {
         $not: { $eq: "American" }
     }
  }
)
```

<hr>

<h2 id="$regex">$regex</h2>

<blockquote>
 <p>$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.</p>

  <p>Find restaurants where the name starts with mor and case insensitive.</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      title: {
        $regex: /mor/,
        $options: "i" // case insensitive option
     } 
  }
)
```

<hr>

<h2 id="$regex">$text and $search</h2>

<blockquote>
 <p>$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.</p>

  <p>Find restaurants where the name starts with morris.</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      $text: { $search: "morris" }
  }
)
```

<hr>

<h2 id="$where">$where</h2>

<blockquote>
 <p>$where allows you to write javascript code directly into the query.</p>

  <p>Find restaurants where the rating divided by two is still higher that 1.5 .</p>
</blockquote>

```bash
db.restaurants.find( 
  {
      $where: function () {
        return this.rating / 2  > 1.5;
      }
  }
)
```

<hr>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ol4s13iwe5v99nahcaq.jpeg)

<hr>
<h2>Next parts</h2>
<ul>
  <li><a href="/">Part three (coming soon) </a></li>
</li>


</ul>