Query Document on MongoDB

Query Document on MongoDB

Instructor-svgAl-Mamun Sarkar
Sep 11 , 2020

In this lesson, I will show you how to query documents of MongoDB collection using various logical operations. 

I will use the employees table for performing queries. 

Find All Document:

db.employees.find()
db.employees.find().pretty()

 

Find One Document:

db.employees.findOne({email:"sina@gmail.com"})

 

Find By Equal (=) Condition:

db.employees.find({email:"sina@gmail.com"})

 

Find (Where salary < 500):

db.employees.find({salary:{"$lt": 500}})

 

Find (Where salary <= 500):

db.employees.find({salary:{"$lte": 500}})

 

Find (Where salary > 500):

db.employees.find({salary:{"$gt": 500}})

 

Find (Where salary >= 500):

db.employees.find({salary:{"$gte": 500}})

 

Find (Where salary != 500):

db.employees.find({salary:{"$ne": 500}})

 

Find (Where salary in [500, 600, 2000] ):

db.employees.find({salary:{"$in": [500, 600, 2000]}})

 

 Find (Where salary not in [500, 600, 2000] ):

db.employees.find({salary:{"$nin": [500, 600, 2000]}})

 

Find( WHERE salary = 500 AND email = 'bos@gmail.com' ):

db.employees.find({ $and: [{"salary": 500}, {"email": 'bos@gmail.com'}] })

 

 Find( WHERE salary = 500 OR email = 'bos@gmail.com' ):

db.employees.find({ $or: [{"salary": 500}, {"email": 'bos@gmail.com'}] })

 

 Find( WHERE salary > 500 OR email = 'bos@gmail.com' ):

db.employees.find({ $or: [{"salary": {$gt:500}}, {"email": 'bos@gmail.com'}] })

 

  Find( WHERE salary > 500 AND salary < 1000 ):

db.employees.find({ $and: [{"salary": {$gt:500}}, {"salary": {$lt:1000}}] })

 

  Find( WHERE NOT salary > 500 ):

db.employees.find({ "salary": { $not: {$gt: 500} } })

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram