Python-data-access-python-mongodb-find
提供:Dev Guides
Python MongoDB-検索
*_find()_* メソッドを使用して、MongoDBから保存されたドキュメントを読み取り/取得できます。 このメソッドは、非構造化された方法でMongoDB内のすべてのドキュメントを取得して表示します。
構文
以下は、 _ find()_ メソッドの構文です。
>db.CollectionName.find()
例
次のクエリを使用して、sampleという名前のコレクション内のtestDBという名前のデータベースに3つのドキュメントを挿入したとします-
> use testDB
> db.createCollection("sample")
> data = [
{"_id": "1001", "name" : "Ram", "age": "26", "city": "Hyderabad"},
{"_id": "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" },
{"_id": "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" }
]
> db.sample.insert(data)
次のようにfind()メソッドを使用して、挿入されたドキュメントを取得できます-
> use testDB
switched to db testDB
> db.sample.find()
{ "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" }
{ "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" }
{ "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" }
>
また、findOne()メソッドを使用してコレクションの最初のドキュメントを取得することもできます-
> db.sample.findOne()
{ "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" }
Pythonを使用したデータの取得(検索)
pymongoの find_One() メソッドは、クエリに基づいて単一のドキュメントを取得するために使用されます。一致しない場合、このメソッドは何も返さず、クエリを使用しない場合、コレクションの最初のドキュメントを返します。
このメソッドは、結果のドキュメントを1つだけ取得する必要がある場合、またはクエリが1つのドキュメントのみを返すことが確実な場合に便利です。
例
次のPythonの例では、コレクションの最初のドキュメントを取得します-
from pymongo import MongoClient
#Creating a pymongo client
client = MongoClient('localhost', 27017)
#Getting the database instance
db = client['mydatabase']
#Creating a collection
coll = db['example']
#Inserting document into a collection
data = [
{"_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad"},
{"_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore"},
{"_id": "103", "name": "Robert", "age": "28", "city": "Mumbai"}
]
res = coll.insert_many(data)
print("Data inserted ......")
print(res.inserted_ids)
#Retrieving the first record using the find_one() method
print("First record of the collection: ")
print(coll.find_one())
#Retrieving a record with is 103 using the find_one() method
print("Record whose id is 103: ")
print(coll.find_one({"_id": "103"}))
出力
Data inserted ......
['101', '102', '103']
First record of the collection:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
Record whose id is 103:
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
1つのクエリで複数のドキュメントを取得するには(単一メソッドのod findメソッド)、pymongoの find() メソッドを使用できます。 クエリを渡していない場合、これはコレクションのすべてのドキュメントを返し、このメソッドにクエリを渡した場合、一致したすべてのドキュメントを返します。
例
#Getting the database instance
db = client['myDB']
#Creating a collection
coll = db['example']
#Inserting document into a collection
data = [
{"_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad"},
{"_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore"},
{"_id": "103", "name": "Robert", "age": "28", "city": "Mumbai"}
]
res = coll.insert_many(data)
print("Data inserted ......")
#Retrieving all the records using the find() method
print("Records of the collection: ")
for doc1 in coll.find():
print(doc1)
#Retrieving records with age greater than 26 using the find() method
print("Record whose age is more than 26: ")
for doc2 in coll.find({"age":{"$gt":"26"}}):
print(doc2)
出力
Data inserted ......
Records of the collection:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
Record whose age is more than 26:
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}