---
title: Python MongoDB Find
description: "In MongoDB we use the `find()` and `find_one()` methods to find data in a collection. MongoDB-д өгөгдлийг цуглуулгаас хайхын тулд `find()` болон `find_one()` аргуудыг ашигладаг. Just like the `SELECT` statement is used to find data in a table in a MySQL database. Яг л MySQL өгөгдлийн…"
url: "https://apprentice.mn/docs/python-mongodb-find/"
updated: "2024-08-05"
category: Python
---

# Python MongoDB Find

In MongoDB we use the `find()` and `find_one()` methods to find data in a collection.

MongoDB-д өгөгдлийг цуглуулгаас хайхын тулд `find()` болон `find_one()` аргуудыг ашигладаг.

Just like the `SELECT` statement is used to find data in a table in a MySQL database.

Яг л MySQL өгөгдлийн санд хүснэгтээс өгөгдлийг олоход `SELECT` командыг ашигладагтай адил.

## Find One

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

MongoDB-д цуглуулгаас өгөгдөл сонгохын тулд бид `find_one()` командыг ашиглаж болно.

       

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

`find_one()` арга нь сонгосон өгөгдлөөс эхний тохиолдлыг буцаадаг.

#### Example – Жишээ

Find the first document in the customers collection:

“customers” цуглуулгад байгаа эхний баримтыг олохын тулд `find_one()` аргыг ашиглана.

`import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]x = mycol.find_one()print(x)`

## Find All – 

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

MongoDB дахь хүснэгтээс өгөгдөл сонгохын тулд `find()` аргыг мөн ашиглаж болно.

       

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

`find()` арга нь сонгосон бүх өгөгдлийг буцаана.

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.

`find()` аргын анхны параметр нь хайлтын объект байдаг. Энэ жишээнд хоосон хайлтын объектыг ашиглаж байгаа бөгөөд энэ нь цуглуулга дахь бүх баримтуудыг сонгох болно.

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

`find()` арга параметргүй үед MySQL-д ашигладаг `SELECT *` командтай адил үр дүнг авах болно.

#### Example – Жишээ

Return all documents in the “customers” collection, and print each document:

“customers” цуглуулгад байгаа бүх баримт бичгийг буцааж, тус бүрээр нь хэвлэх:

`import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]for x in mycol.find():  print(x)`

## Return Only Some Fields – Зөвхөн зарим талбаруудыг буцаах

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

`find()` функцийн хоёр дахь параметр нь үр дүнгээс оруулах талбаруудыг тодорхойлсон объект юм.

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

Энэ параметр нь заавал шаардлагатай биш бөгөөд хэрэв орхигдсон бол бүх талбарууд үр дүнд багтана.

#### Example – Жишээ

Return only the names and addresses, not the _ids:

Зөвхөн нэрнүүд болон хаягнуудыг буцаана уу, _ids орохгүйгээр:

`import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }):  print(x)`

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:

#### Example – Жишээ

This example will exclude “address” from the result:

Энэ жишээний үр дүн нь “хаяг”-г хасна:  

`import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]for x in mycol.find({},{ "address": 0 }):  print(x)`

##### Example

You get an error if you specify both 0 and 1 values in the same object (except if one of the fields is the _id field):

`import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]for x in mycol.find({},{ "name": 1, "address": 0 }):  print(x)`

               [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mongodb-find%2F&linkname=Python%20MongoDB%C2%A0Find](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mongodb-find%2F&linkname=Python%20MongoDB%C2%A0Find)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mongodb-find%2F&linkname=Python%20MongoDB%C2%A0Find](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mongodb-find%2F&linkname=Python%20MongoDB%C2%A0Find)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mongodb-find%2F&linkname=Python%20MongoDB%C2%A0Find](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mongodb-find%2F&linkname=Python%20MongoDB%C2%A0Find)[https://www.addtoany.com/share](https://www.addtoany.com/share)
