Python Tutorials · Python MongoDB

MongoDB Find

Learn all about MongoDB Find in this comprehensive tutorial.

5 min read advanced
  • To select data from a collection in MongoDB, we can use the find_one() method.
  • To select data from a table in MongoDB, we can also use the find() method.
  • The second parameter of the find() method is an object describing which fields to include in the result.

Introduction

Note: In MongoDB we use the find() and find_one() methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database.

Find One

To select data from a collection in MongoDB, we can use the find_one() method.

The find_one() method returns the first occurrence in the selection.

python

Find All

To select data from a table in MongoDB, we can also use the find() method.

The find() method returns all occurrences in the selection.

The first parameter of the find() method is a query object. In this example we use an empty query object, which selects all documents in the collection.

Note: No parameters in the find() method gives you the same result as SELECT * in MySQL.
python

Return Only Some Fields

The second parameter of the find() method is an object describing which fields to include in the result.

This parameter is optional, and if omitted, all fields will be included in the result.

python
Note: You are not allowed to specify both 0 and 1 values in the same object (except if one of the fields is the _id field). If you specify a field with the value 0, all other fields get the value 1, and vice versa:
python
python

Module quiz

2 questions
1

Which of the following is true about MongoDB Find?

2

What is the most common pitfall when working with MongoDB Find?

Answer all questions to submit.