MongoDB Find, Operators, And Filters

In this tutorial, we will see how to import JSON data to a MongoDB Database (collection). We will perform some operations with find and findOne methods. We also will have some queries with Comparison and Logical Operators.

In short, we will cover the following topics.

  • Import Data
  • Findone() and Find() methods
  • Comparison Operators ($get, $ne, $in, $nin)
  • Logical Operators ($and, $or, $not, $nor)

Import Data

Firstly, we will import JSON data to our MongoDB database. For this, we will require a JSON dataset. You can get this sample JSON dataset from the following link.

https://raw.githubusercontent.com/prust/wikipedia-movie-data/master/movies.json

Store the above JSON dataset into a file on your desktop. We will import this dataset to our MongoDB database.

Before importing the dataset, you must start the Mongo Server on one terminal (mongod --dbpath “C:\Program Files\MongoDB\Server\4.0\data\db”).

Now, open another terminal and go to the Desktop directory path (JSON file path) and write the below command.

Syntax

mongoimport <NameOfJsonFile> -d <DatabaseName> -c <CollectonName> --jsonArray --drop

 ( -d = for database

-c = for collection

--jsonArray = to import multiple documents

--drop = to drop existing database or collection and if you want to append the data then do not use this flag) 

cmd> mongoimport movies.json –d dbmovies –c movies --jsonArray --drop

MongoDB Find, Operators And Filters 

You can check above that it has successfully imported all the documents!

You can also check your database by running the following commands.

  1. show dbs
  2. use dbmovies
  3. show collections
  4. movies.find().pretty();

These will return all your documents like this.

MongoDB Find, Operators And Filters 

This will return the cursor and it will return the first 20 documents on your screen.

findOne() and find() methods

Syntax

db.collection.findone();
cmd> db.movies.findOne();

MongoDB Find, Operators And Filters 

This will return only one single document from the collection. Now, we can also pass a query to the findOne() method and then, it will return only one document which will match that criteria.

For example, we want only that document which has year=2018.

cmd> db.movies.findOne({year:2018});

MongoDB Find, Operators And Filters 

The above code will return one document that satisfies the specified query criteria on the collection. If multiple documents satisfy the query, this method returns the first document according to the natural order of documents.

But now, what if we want all those documents from the collection which will match our given criteria? Well, for that, you can use the find() method instead of the findOne() method.

cmd> db.movies.find({year:2018});

MongoDB Find, Operators And Filters 

It will return all the documents which will fulfill the given criteria. Note that it will return documents in the form of a cursor.

So, in short, the basic difference between findOne() and find() is as below.

  • findOne - if query matches, the first document is returned, otherwise null.
  • find – no matter how many documents matched, a cursor is returned; never null.

Now, it’s time to move to the operators.

Operators

MongoDB offers the following operators.

Query and Projection Operators

Query operators provide ways to locate data within the database and projection operators modify how data is presented.

Update Operators

Update operators enable you to modify the data in your database or add additional data.

Aggregation Pipeline Stages

Available aggregation stages for Aggregation Pipeline.

Aggregation Pipeline Operators

Aggregation pipeline operations have a collection of operators available to define and manipulate documents in pipeline stages.

Query Modifiers

Query modifiers determine the way that queries will be executed.

 In this tutorial, we are going to check Query and Projection operators.

 Here is the link to the official MongoDB document where you can refer to all the operators.

https://docs.mongodb.com/manual/reference/operator/query/

So, inside a Query Selector, we can have the following operators.

MongoDB Find, Operators And Filters 

Now, let’s see some of these operators one by one.

Comparison Operators

 
$eq

$eq: Matches values that are equal to a specified value.

By continuing the above example, let’s get those movies which were released in the year 2011. So, the command would be like below.

cmd> db.movies.find({year:{$eq:2011}}).pretty();

MongoDB Find, Operators And Filters 

As you can see, it will return all the documents from movies collection which are having year=2011.

And yes, this is exactly the same find() command which we previously used.

OK, so let’s move on to the next operator.

$gt

$gt: Matches values that are greater than a specified value

cmd> db.movies.find({year:{$gt:2011}}).pretty();

MongoDB Find, Operators And Filters 

You can see that it will return all the movies which were released after the year 2011.

$nin

$nin: Matches none of the values specified in an array

Here, we want to list those movies which do not have genres like Comedy, Drama, and Action.

cmd> db.movies.find({genres:{$nin:['Comedy','Drama','Action']}}).pretty();

MongoDB Find, Operators And Filters 
 

Logical Operators

 
$and

Now, if we want to pass multiple criteria or filters on this find() method, then we can -

use $and operator.

So here, we want to get those movies which do not have genres like Drama and Thriller and those movies should be released in the year 2018.

cmd> db.movies.find({$and:[{genres:{$nin:['Drama','Thriller']}},{year:2018}]}).pretty();

MongoDB Find, Operators And Filters 

As you can see, we have passed an array to our $and operator and inside this array, we can use multiple filters.

So, that’s all for this tutorial. I hope you found this tutorial helpful! Thank you.


Similar Articles