MongoDB Part-II (Find,Operators and Filters)

jinal shah
5 min readMar 5, 2019

Hello readers,

In this tutorial we will see how to import existing json data to mongoDB Database (Collection). We will perform some operations with find and findOne methods. And we will also have some queries with Comparison and Logical Operators.

In short we will cover following topics:

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

1) Import Data

First, we will import json data to our MongoDB database. So 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 above json dataset into a file on your machine and then we will import this data set to our MongoDB database.

Before importing dataset, you must start mongo server on one terminal (mongod — dbpath “C:\Program Files\MongoDB\Server\4.0\data\db”).you can have more details on how to getting started and all here.

Now, open another terminal and go up to path where you have store the file(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

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

You can also check your database by running following commands:

1. show dbs

2. use dbmovies

3. show collections

4. db.movies.find().pretty();

These will return your all documents like this:

This will return cursor and at a time it will return first 20 documents on your screen.

2) findOne() and find() methods:

Syntax:

db.collection.findone();

cmd> db.movies.findOne();

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});

Above 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. So for that you can use find() method instead of findOne() method:

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

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

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

  • findOne — if query matches, first document is returned, otherwise null.
  • find — no matter number of documents matched, a cursor is returned, never null.

So, now it’s time to move to the operators.

3) Operators:

MongoDB offers 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 are operators that 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 official MongoDB document where you can refer all the operators:

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

So, inside a Query Selectors we can have following operators:

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 above example let’s get those movies which were released in the year 2011. So the command would be:

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

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 a value that are greater than a specified value

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

So you can see it will return all the movies which were released after 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();

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();

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 it helpful! Thank you.

--

--

jinal shah

Jinal Shah is corporate trainer on different technology like node.Js, Angular,Ionic 2, BOT Framework etc.