> For the complete documentation index, see [llms.txt](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/nosql/mongodb/querying-mongodb.md).

# Querying MongoDB

2 ways to view the entire contents of collection

<pre><code><strong>db.getCollection("friendsCollection").find()
</strong>
db.friendsCollection.find()
</code></pre>

**Filter Documents**

```
// exact match

db.friendsCollection.find(
{
    age: 31
}
)

// less than

db.friendsCollection.find(
{
    age : {$lt : 31}
}
)

// greater than

db.friendsCollection.find(
{
    age : {$gt : 30}
}
)

// Between

db.friendsCollection.find(
{
    age : {$gt : 29},
    age : {$lt : 31}
}
)

// nested

db.friendsCollection.find({"name.firstname":"Ben"});
```

**More Queries**

**Using Regular Expression**&#x20;

Regular expressions are used within two slashes /  /

```
// Find firstnames containing letter R

db.friendsCollection.find({"firstname": /.*R.*/})
or
db.friendsCollection.find({"firstname": /R/})
```

```
// Find names starting with lower case r

db.friendsCollection.find({"firstname": /^r/})

// Find names ending with e

db.friendsCollection.find({"firstname": /e$/})

// case insensitive

db.friendsCollection.find({"firstname":{'$regex' : '^r', '$options' : 'i'}})

db.friendsCollection.find({"firstname":{"$regex" : "^r", "$options" : "i"}})
```
