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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.