Querying in MongoDB Compass

  • Read
  • Discuss

What is MongoDB Query?

MongoDB Query is a way to get the data from the MongoDB database. Similar to SQL queries in the SQL Database language, MongoDB query makes the process of obtaining data from the database simple. One can add criteria or conditions to a mongodb query operation in order to retrieve specific data from the database.

What is MongoDB Compass?

MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment.

Compass MongoDB is free to use and source available, and can be run on macOS, Windows, and Linux.

Querying using Filter

You can type MongoDB filter documents into the query bar to display only documents which match the specified criteria. 

Select All Documents in a Collection

To select all documents in the collection, pass an empty document as the query filter parameter to the query bar. The query filter parameter determines the select criteria.

This operation uses a filter predicate of {}, which corresponds to the following SQL statement:

SELECT * FROM Pets;

Specify Equality Condition

To specify equality conditions, use <field>:<value> expressions in the query filter document.

The following example selects from the Pets collection all documents where the age is greater than 5:

{Age:{$gte:5}}

Copy the above filter into the Compass query bar and click Find:

This operation uses a filter predicate of { Age:{$gte:5} }, which corresponds to the following SQL statement:

SELECT * FROM Pets WHERE Age>5;

Specify Multiple Conditions Using Query Operators

A query filter document can use the query operators to specify conditions in the following form:

{ <field1>: { <operator1>: <value1> }, ... }

The following example retrieves all documents from the Pets collection where Age is either 6 or 10 years:

{Age:{$in:[6,10]}}

Copy the above filter into the Compass query bar and click Find:

The operation uses a filter predicate of { Age: { $in: [ 6, 10 ] } }, which corresponds to the following SQL statement:

SELECT * FROM Pets WHERE Age in (6,10);

Specify AND Conditions

A compound query can specify conditions for more than one field in the collection’s documents. A compound query’s clauses are implicitly connected by a logical AND conjunction in order to choose the documents in the collection that satisfy all the conditions.

The following example retrieves all documents in the Pets collection where the Age is less than ($lt) 10 and Kind is dog:

{Age:{$lt:10},Kind:"Dog"}

Copy the above filter into the Compass query bar and click Find:

The operation uses a filter predicate of {Age:{$lt:10},Kind:”Dog”}, which corresponds to the following SQL statement:

SELECT * FROM Pets WHERE Age<10 AND Kind=Dog;

Specify OR Conditions

Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.

The following example retrieves all documents in the collection where the Kind is Dog or Age<10:

{ $or: [{Age:{$lt:10},Kind:"Dog"}]}

Copy the above filter into the Compass query bar and click Find:

The operation uses a filter predicate of { $or: [{Age:{$lt:10},Kind:”Dog”}]}, which corresponds to the following SQL statement:

SELECT * FROM Pets WHERE Kind="DOG" OR Age<10;

Specify AND as well as OR Conditions

In the following example, the compound query document selects all documents in the collection where the Kind equals “Dog” and either Age is less than ($lt) 30 or Gender is Female

{Kind:"Dog",$or:[{Age:{$lt:10}},{Gender:"Female"}]}

Copy the above filter into the Compass query bar and click Find:

The operation uses a filter predicate of:

{
  Kind:"Dog"
  $or: [
    { Age: { $lt: 30 } }, { Gender:"Female"}
  ]
}

which corresponds to the following SQL statement:

SELECT * FROM Pets WHERE Kind="Dog  AND ( Age < 10 OR Gender="Female");

Leave a Reply

Scroll to Top