Docs Menu
Docs Home
/
MongoDB Compass
/

Query Your Data

On this page

  • Compatibility
  • Set Query Filter
  • Examples
  • Match by a Single Condition
  • Match by Multiple Conditions ($and)
  • Match by Multiple Possible Conditions ($or)
  • Match by Exclusion ($not)
  • Match with Comparison Operators
  • Match by Date
  • Match by Array Conditions
  • Match by Substring
  • Match by Embedded Field
  • Supported Data Types in the Query Bar
  • Clear the Query
  • Query Collections with Invalid UTF8 Data
  • How Does the Compass Query Compare to MongoDB and SQL Queries?

You can type MongoDB filter documents into the query bar to display only documents which match the specified criteria. To learn more about querying documents, see Query Documents in the MongoDB manual.

You can query your data for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB

  • MongoDB Community: The source available, free-to-use, and self-managed version of MongoDB

To learn more about querying your data for deployments hosted in MongoDB Atlas, see Find Specific Documents.

  1. In the Filter field, enter a filter document between the curly braces. You can use all the MongoDB query operators except the $text and $expr operators.

    Example

    The following filter returns documents that have a title value of Jurassic Park:

    { "title": "Jurassic Park" }
  2. Click Find to run the query and view the updated results.

    Results of applying a query filter
    click to enlarge

The examples on this page use a small example dataset. To import the sample data into your MongoDB deployment, perform the following steps:

  1. Copy the following documents to your clipboard:

    [
    {
    "name": "Andrea Le",
    "email": "andrea_le@fake-mail.com",
    "school": {
    "name": "Northwestern"
    },
    "version": 5,
    "scores": [ 85, 95, 75 ],
    "dateCreated": { "$date": "2003-03-26" }
    },
    {
    "email": "no_name@fake-mail.com",
    "version": 4,
    "scores": [ 90, 90, 70 ],
    "dateCreated": { "$date": "2001-04-15" }
    },
    {
    "name": "Greg Powell",
    "email": "greg_powell@fake-mail.com",
    "version": 1,
    "scores": [ 65, 75, 80 ],
    "dateCreated": { "$date": "1999-02-10" }
    }
    ]
  2. In Compass, use the left navigation panel to select the database and the collection you want to import the data to.

  3. Click the Documents tab.

  4. Click Add Data and select Insert Document.

  5. Set the View to JSON ({}).

  6. Paste the JSON documents from your clipboard into the modal.

  7. Click Insert.

Note

If you do not have a MongoDB deployment or if you want to query a larger sample data set, see Sample Data for Atlas Clusters for instructions on creating a free-tier cluster with sample data. The following example queries filter the sample documents provided on this page.

The following query filter finds all documents where the value of name is "Andrea Le":

{ name: "Andrea Le" }

The query returns the following document:

{
"_id": { "$oid": "5e349915cebae490877d561d" },
"name": "Andrea Le",
"email": "andrea_le@fake-mail.com",
"school": {
"name": "Northwestern"
},
"version": 5,
"scores": [ 85, 95, 75 ],
"dateCreated": { "$date": "2003-03-26" }
}

The following query filter finds all documents where scores array contains the value 75, and the name is Greg Powell:

{ $and: [ { scores: 75, name: "Greg Powell" } ] }

The query returns the following document:

{
"_id": { "$oid":"5a9427648b0beebeb69579cf" },
"name": "Greg Powell",
"email": "greg_powell@fake-mail.com",
"version": 1,
"scores": [ 65, 75, 80 ],
"dateCreated": { "$date": "1999-02-10" }
}

The following query filter uses the $or operator to find documents where version is 4, or name is Andrea Le:

{ $or: [ { version: 4 }, { name: "Andrea Le" } ] }

The query returns the following documents:

[
{
"_id": { "$oid": "5e349915cebae490877d561d" },
"name": "Andrea Le",
"email": "andrea_le@fake-mail.com",
"school": {
"name": "Northwestern"
},
"version": 5,
"scores": [ 85, 95, 75 ],
"dateCreated": { "$date": "2003-03-26" }
},
{
"_id": { "$oid":"5e349915cebae490877d561e" },
"email": "no_name@fake-mail.com",
"version": 4,
"scores": [ 90, 90, 70 ],
"dateCreated": { "$date": "2001-04-15" }
}
]

The following query filter uses the $not operator to find all documents where the value of the name field is not equal to "Andrea Le", or the name field does not exist:

{ name: { $not: { $eq: "Andrea Le" } } }

The query returns the following documents:

[
{
"_id": { "$oid":"5e349915cebae490877d561e" },
"email": "no_name@fake-mail.com",
"version": 4,
"scores": [ 90, 90, 70 ],
"dateCreated": { "$date": "2001-04-15" }
},
{
"_id": { "$oid":"5a9427648b0beebeb69579cf" },
"name": "Greg Powell",
"email": "greg_powell@fake-mail.com",
"version": 1,
"scores": [ 65, 75, 80 ],
"dateCreated": { "$date": "1999-02-10" }
}
]

Tip

See also:

For a complete list of logical query operators, see Logical Query Operators.

The following query filter uses the $lte operator to find all documents where version is less than or equal to 4:

{ version: { $lte: 4 } }

The query returns the following documents:

[
{
"_id": { "$oid":"5e349915cebae490877d561e" },
"email": "no_name@fake-mail.com",
"version": 4,
"scores": [ 90, 90, 70 ],
"dateCreated": { "$date": "2001-04-15" }
},
{
"_id": { "$oid":"5a9427648b0beebeb69579cf" },
"name": "Greg Powell",
"email": "greg_powell@fake-mail.com",
"version": 1,
"scores": [ 65, 75, 80 ],
"dateCreated": { "$date": "1999-02-10" }
}
]

Tip

See also:

For a complete list of comparison operators, see Comparison Query Operators.

The following query filter uses the $gt operator and Date() method to find all documents where the dateCreated field value is later than June 22nd, 2000:

{ dateCreated: { $gt: new Date('2000-06-22') } }

The query returns the following documents:

[
{
"_id": { "$oid": "5e349915cebae490877d561d" },
"name": "Andrea Le",
"email": "andrea_le@fake-mail.com",
"school": {
"name": "Northwestern"
},
"version": 5,
"scores": [ 85, 95, 75 ],
"dateCreated": { "$date": "2003-03-26" }
},
{
"_id": { "$oid": "5e349915cebae490877d561e" },
"email": "no_name@fake-mail.com",
"version": 4,
"scores": [ 90, 90, 70 ],
"dateCreated": { "$date": "2001-04-15" }
}
]

The following query filter uses the $elemMatch operator to find all documents where at least one value in the scores array is greater than 80 and less than 90:

{ scores: { $elemMatch: { $gt: 80, $lt: 90 } } }

The query returns the following document because one of the values in the scores array is 85:

{
"_id": { "$oid": "5e349915cebae490877d561d" },
"name": "Andrea Le",
"email": "andrea_le@fake-mail.com",
"school": {
"name": "Northwestern"
},
"version": 5,
"scores": [ 85, 95, 75 ],
"dateCreated": { "$date": "2003-03-26" }
}

For more query examples, see Query Documents in the MongoDB manual.

The following query filter uses the $regex operator to find all documents where the value of email includes the term "andrea_le":

{ email: { $regex: "andrea_le" } }

The query returns the following document:

{
"_id": { "$oid": "5e349915cebae490877d561d" },
"name": "Andrea Le",
"email": "andrea_le@fake-mail.com",
"school": {
"name": "Northwestern"
},
"version": 5,
"scores": [ 85, 95, 75 ],
"dateCreated": { "$date": "2003-03-26" }
}

The following query filter finds the document with the school.name subfield of "Northwestern":

{ "school.name": "Northwestern" }

The query returns the following document:

{
"_id": { "$oid": "5e349915cebae490877d561d" },
"name": "Andrea Le",
"email": "andrea_le@fake-mail.com",
"school": {
"name": "Northwestern"
},
"version": 5,
"scores": [ 85, 95, 75 ],
"dateCreated": { "$date": "2003-03-26" }
}

For more query examples, see Query Documents in the MongoDB manual.

The Compass Filter supports using the mongo shell mode representation of the MongoDB Extended JSON BSON data types.

Example

The following filter returns documents where start_date is greater than than the BSON Date 2017-05-01:

{ "start_date": {$gt: new Date('2017-05-01')} }

By specifying the Date type in both start_date and the $gt comparison operator, Compass performs the greater than comparison chronologically, returning documents with start_date later than 2017-05-01.

Without the Date type specification, Compass compares the start_dates as strings lexicographically, instead of comparing the values chronologically.

To clear the query bar and the results of the query, click Reset.

If you attempt to query or export data with invalid UTF8 characters the following error message displays:

Invalid UTF-8 string in BSON document.

To query or export this data, disable UTF8 validation by setting the enableUtf8Validation URI option to false.

Warning

Editing data with enableUtf8Validation=false can result in loss of data. This approach is a temporary workaround to query or export data only.

The following URI disables UTF8 validation:

mongodb://localhost:27017/?enableUtf8Validation=false

Note

You can also disable this option in the Advanced Connection Options by selecting enableUtf8Validation and entering false.

$filter corresponds to the WHERE clause in a SQL SELECT statement.

Example

You have 3,235 articles. You would like to see all articles that Joe Bloggs wrote.

Compass Filter Option
{ author : { $eq : "Joe Bloggs" } }
MongoDB Aggregation
db.article.aggregate( { $match: { "author": "Joe Bloggs" } } )
SQL
SELECT * FROM article
WHERE author = "Joe Bloggs";

Back

Delete Multiple Documents