Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

MongoDB Introduction

MongoDB Setup

MongoDB DataBase

MongoDB Insert Data

MongoDB Read Document

MongoDB Update

MongoDB Delete

MongoDB Sorting Index

MongoDB Aggregation

MongoDB Mongodb with python

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Read mongodb documents

Suppose you have a data base name first collection. In the collection, you have keys like Books and price. In the Books, you have books name like python, java, javascript, sql, etc.

How to see all the document present in a collection?

find() function

Syntax:
db.collection_name.find()

Example:
db.first_collection.find()

For a better representation of document we can use pretty() function.

Syntax:
db.collection_name.find().pretty()

Example:
db.first_collection.find().pretty()

How to get values using condition?

Syntax:
db.collection_name.find(query,projection)

Condition example 1:Get all the document of that collection where we have python?

Here you have to pass value as key:value pair in the query. So to get, write python key name which is Books and value which is python. Here you have to write(capital or small letter, space, etc) the same thing what we wrote while inert values.

Example:
db.first_collection.find({Books:"Python"}).pretty()

Condition example 2:Get only python, don't show other documents present in the collection where we have python?

To get we have to give comma after query and have to write the key and then type in front of the key 0 or 1. If we write 1 then only the given key name and value will be displayed and if we write 0 then other keys and values will be displayed and the given key and value will not displayed.

Example 1:
db.first_collection.find({Books:"Python"},name:1).pretty()
Example 2:
db.first_collection.find({Books:"Python"},name:0).pretty()

While reading we are getting a id but we don't need that.
How to ignore the id column while reading documents?

Example:
db.first_collection.find({Books:"Python"},{_id=0,name:1}).pretty()

How to use limit() function?

Condition example 3:Get first or second or a specified number of document if too many documents are get matched?
Suppose we write a query where python is written in name and we want that document. Now we have 5 python documents. So normally what will happen, it will display all the python documents. To get a specified number of documents we use the limit function. Inside the limit function, we write the number of the document that we want. To get one document we write 1, for 4 we write 4. Now the selection will start from first. If we write 1 then it doesn't matter how many documents we have named python, it will select the first document from all the document of python. Now if we want 7 documents then it will select the first 7 documents of python.

Example 1:
db.first_collection.find({Books:"Python"}).pretty().limit(1)
Example 2:
db.first_collection.find({Books:"Python"}).pretty().limit(3)

How to use skip() function?

Now see one thing, suppose 10 documents are get matched and we want a document which is in number three position or number 6 position. Now if we only use limit then this function will start o selecting from the beginning. But we want to start selecting after a specified number of the document. To do this, we will use skip after the limit function. In skip function, we also pass numbers. If we pass 5 then after 5 documents, limit function will starts to select.

You can also use skip function without limit function. If you use only skip function then the output will be same.

Example 1: With limit function
db.first_collection.find({Books:"Python"}).pretty().limit(1).skip(3)
Example 2: Without limit function
db.first_collection.find({Books:"Python"}).pretty().skip(2)

Queries

Queries are some finding methods of MongoDB with operators.
The operators are:
1.Eq---> equal
2.lt---> less than
3.lte-->less than equal
4.gte--> greater than equal
5.gt--->greater than
6.ne--->not equal
7.and
8.or

By using these operators you can use conditions and according to the condition you will get documents.

Let's see some example:
Suppose you have a collection named user, where you have keys like salary, job title, city name.

1. Find all the document which contain city name New York.

db.user.find({"city":"New York"})
2. Find all the document which contain salary less than 25000.

db.user.find({"salary":{$lt:25000}})
3. Find all the document which contain salary greater than equal 80000.

db.user.find({"salary":{$gte:80000}})

Use of AND?

Using AND you can use multiple conditions and when all the conditions are true only then you will get the output.

Let's see some example using AND
Question 1:
Suppose you have a collection where you have keys like City name, Salary and Title. Now you want to get those documents which city name is New York and salary is 25k.
Look here you have two conditions one is for city name and other is for salary and you want that, both conditions need to be true to get the output.

db.users.find({$and:[{"city":"New York"},{"salary":25000}]})

Here the user is the collection name. To use and at first inside find function we have to write dollar sign then write and, then a colon. If you have more conditions then give a comma and inside the second bracket pass the key and value.

Question 2:
Now find all the documents which contain salary greater than 25000 and city is New York. Use the question 1 database.

db.user.find({$and:[{"city":"New York","salary":{$gt:25000}}]})

Use of OR?

Using OR you can use multiple conditions and when a single condition is true only then you will get the output.

Let's see some example using OR
Question 1:
Suppose you have a collection where you have keys like City name, Salary and Title. Now you want to get those documents which city name is New York or salary is 25000.
Look here you have two conditions one is for city name and other is for salary and you want only that at least one condition is needed to true to get the output.

db.users.find({$or:[{"city":"New York"},{"salary":25000}]})

Here the user is the collection name. To use and at first, inside find function, you have to write dollar sign then write or and then a colon.

Question 2:
Find all the documents which contain salary greater than 25000 or city New York.

db.user.find({$or:[{"salary":{$gt:25000},"city":"New York"}]})

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.