How do i filter in mongodb?

Docs HomeMongoDB Compass

On this page

  • Set Query Filter
  • Supported Data Types in the Query Bar
  • Clear the Query
  • How Does the Compass Query Compare to MongoDB and SQL Queries?
  • Examples

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.

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

    Example

    The following filter only returns documents which have a Country value of Brazil:

  2. Click Find to run the query and view the updated results.

    How do i filter in mongodb?

    click to enlarge

Note

For query result sets larger than 1000 documents, Compass shows a sampling of the results. Otherwise, Compass shows the entire result set.

For details on sampling, see Sampling.

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.

$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(
{ $filter : { author : { $eq : "Joe Bloggs" } } }
);

SQL

SELECT * FROM article
WHERE author = "Joe Bloggs";

Example

The following examples use the JSON documents below as sample data. To import this sample data to your MongoDB deployment with MongoDB Compass:

  1. Copy the array of documents below by clicking Copy.

[
{
"name":"Andrea Le",
"email":"",
"version":5,
"scores":[85, 95, 75],
"dateCreated":{"$date":"2003-03-26"}
},
{
"email":"",
"version":4,
"scores":[90, 90, 70],
"dateCreated":{"$date":"2001-04-15"}
},
{
"name":"Greg Powell",
"email":"",
"version":1,
"scores":[65, 75, 80],
"dateCreated":{"$date":"1999-02-10"}
}
]

  1. In Compass, use the left navigation panel to select the database and the collection you want to import the data to.

  2. Click the Documents tab.

  3. Click Add Data and select Insert Document.

  4. Ensure that View is set to JSON, or {}, and paste the copied JSON documents in the field.

  5. Click Insert.

Note

If you do not have a MonogDB deployment or if you would like to query a large sample data set, see Sample Data for Atlas Clusters for instructions on creating a free-tier cluster with sample data. Note that the examples below are intended to filter the sample JSON documents provided on this page and may not properly filter another sample data set.

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

How do I filter records in MongoDB?

In the Filter field, enter a filter document. You can use all of the MongoDB query operators except the $text and $expr operators. Example. The following filter only returns documents which have a Country value of Brazil : { Country: "Brazil" }.
Click Find to run the query and view the updated results. click to enlarge..

How do I search for a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How do I search for an object in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object. Here is the query to search in an array of objects in MongoDB.

How do I filter data in MongoDB using node JS?

The find() method is also used to filter the result on a specific parameter. You can filter the result by using a query object..
var http = require('http');.
var MongoClient = require('mongodb'). ... .
MongoClient. ... .
if (err) throw err;.
var query = { address: /^L/ };.