How to select data from collections in MongoDB

Hello programmers, today we are going to learn how to select the data from collections in MongoDB using find method in Python. The find method is similar to the SELECT statement used to find data in a table in a MySQL database.

Before moving forward you should check out our previous tutorials on creating a database and some more which are listed below.

  1. How to create a database in MongoDB using Python
  2. How to create a collection and insert data into collection in MongoDB using Python

In MongoDB, we use find and find_one methods to find data from a collection.

Select one data from the collection in MongoDB – Python

To select single data from the collection we use find_one().

resultFind = courses.find_one()
print(resultFind)

Output

{'_id': ObjectId('5dee3c828d178a953eec2ad7'), 'author': 'Shailesh Bhimanpelli', 'age': 21, 'course': 'MongoDB using Python', 'rating': 1}

The output will have first document from the collection.

Returns all fields from the document in MongoDB

To get all the documents inside the collection we use find() method.

for i in courses.find():
    print(i)

Output

{'_id': ObjectId('5dee3c828d178a953eec2ad7'), 'author': 'Shailesh Bhimanpelli', 'age': 21, 'course': 'MongoDB using Python', 'rating': 1}
{'_id': ObjectId('5dee451be59a39bf5e67a6f9'), 'author': 'Saruque Ahamed Mollick', 'age': 25, 'course': 'Python, Java, C++', 'rating': 5}
{'_id': ObjectId('5dee451be59a39bf5e67a6fa'), 'author': 'Ben Thomas', 'age': 23, 'course': 'HTML, CSS', 'rating': 3}
{'_id': ObjectId('5dee4556df618453e14b243c'), 'author': 'Varun Thakur', 'age': 25, 'course': 'JavaScript', 'rating': 5}

To print all the documents in the collection we have to make use of a for loop.

We might require only specific fields from the document to perform some operations for that purpose we need to retrieve the fields which are required.

for i in courses.find({}, {'author':1, 'rating': 1}):
    print(i)

Output

{'_id': ObjectId('5dee3c828d178a953eec2ad7'), 'author': 'Shailesh Bhimanpelli', 'rating': 1}
{'_id': ObjectId('5dee451be59a39bf5e67a6f9'), 'author': 'Saruque Ahamed Mollick', 'rating': 5}
{'_id': ObjectId('5dee451be59a39bf5e67a6fa'), 'author': 'Ben Thomas', 'rating': 3}
{'_id': ObjectId('5dee4556df618453e14b243c'), 'author': 'Varun Thakur', 'rating': 5}

Here we are telling the find function to print only author and rating by making them 1, and other elements by default become 0 except _id. If you do not want to print the _id then we can do ‘_id’: 0. By doing this _id will not be printed in the output.

for i in courses.find({}, {'_id':0, 'author':1, 'rating': 1}):
    print(i)

Output

{'author': 'Shailesh Bhimanpelli', 'rating': 1}
{'author': 'Saruque Ahamed Mollick', 'rating': 5}
{'author': 'Ben Thomas', 'rating': 3}
{'author': 'Varun Thakur', 'rating': 5}

As you can see here the output no more consists of _id.

The complete code is as given below.

from pymongo import MongoClient

mongo = MongoClient('mongodb://localhost:27017')

# CodeSpeedy is the name of the database
db = mongo.CodeSpeedy

# database for courses offered by CodeSpeedy i.e. courses is the name of the collection
courses = db.courses

# inserting single data
course = {
    'author': 'Shailesh Bhimanpelli',
    'age': 21,
    'course': 'MongoDB using Python',
    'rating': 1
}
result = courses.insert_one(course)

# inserting mulitple data
courseMany = [
    {'author': 'Saruque Ahamed Mollick',
    'age': 25,
    'course': 'Python, Java, C++',
    'rating': 5},
    {'author': 'Ben Thomas',
    'age': 23,
    'course': 'HTML, CSS',
    'rating': 3},
    {'author': 'Varun Thakur',
    'age': 25,
    'course': 'JavaScript',
    'rating': 5},
]
resultMany = courses.insert_many(courseMany)

# find_one() method to get single output
resultFind = courses.find_one()
print(resultFind)

# prints all the documents in the collection
for i in courses.find():
    print(i)

# prints only author and rating of all documents
for i in courses.find({}, {'_id':0, 'author':1, 'rating': 1}):
    print(i)

Leave a Reply