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 pymongo
myclient = 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 pymongo
myclient = 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 pymongo
myclient = 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 pymongo
myclient = 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 pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({},{ "name"1"address"0 }):
  print(x)

Powered by BetterDocs

Leave a Reply