Python MongoDB 查找
你可以使用 find() 方法从MongoDB读取/检索存储的文档。该方法以非结构化的方式检索并显示MongoDB中的所有文档。
语法
以下是 find() 方法的语法。
>db.CollectionName.find()
例子
假设我们在一个名为testDB的数据库中插入了3个文档,并在一个名为sample的集合中使用了以下查询方式 −
> 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检索数据(find)
pymongo的 ** find_One()** 方法是用来根据你的查询检索单个文档的,如果没有匹配,这个方法不返回任何东西,如果你没有使用任何查询,它返回集合中的第一个文档。
当你需要在一个结果中只检索一个文档时,或者当你确定你的查询只返回一个文档时,这个方法就很方便了。
例子
下面的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'}
为了在一次查询中获得多个文档(单次调用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'}