
一、【连接mongo服务】、【连接数据库】、【连接集合】
#一.【连接Mongo】
import pymongo
#方法一
client = pymongo.MongoClient(host='localhost', port=27017)
#方法二
client = MongoClient('mongodb://localhost:27017/')
#二.【连接mongo指定数据库’test ‘】:
#方法一
db = client.test
#方法二
db = client['test']
#三.【连接指定集合(Collection)如:students】
#方法一
collection = db.students
#方法二
collection = db['students']
二、【插入数据新版本】在用:
'''
实际上在 PyMongo 3.X 版本中,insert() 方法官方已经不推荐使用了,当然继续使用也没有什么问题,官方推荐使用 insert_one() 和 insert_many() 方法将插入单条和多条记录分开
'''
# A.【insert_one()
student = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
result = collection.insert_one(student)
print(result)
print(result.inserted_id)
'''
运行结果:
<pymongo.results.InsertOneResult object at 0x10d68b558>
5932ab0f15c2606f0c1cf6c5
返回结果和 insert() 方法不同,这次返回的是InsertOneResult 对象,我们可以调用其 inserted_id 属性获取 _id
''' #B.【insert_many() 方法,我们可以将数据以列表形式传递即可】
student1 = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
student2 = {
'id': '',
'name': 'Mike',
'age': 21,
'gender': 'male'
}
result = collection.insert_many([student1, student2])
print(result)
print(result.inserted_ids)
'''
insert_many() 方法返回的类型是 InsertManyResult,调用inserted_ids 属性可以获取插入数据的 _id 列表,运行结果:
<pymongo.results.InsertManyResult object at 0x101dea558>
[ObjectId('5932abf415c2607083d3b2ac'), ObjectId('5932abf415c2607083d3b2ad')]
'''
二、插入数据老版本(不建议用):
#四.【插入数据,在students内,用字典】
student = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
result = collection.insert(student)
print(result)
'''
在 MongoDB 中,每条数据其实都有一个 _id 属性来唯一标识,如果没有显式指明 _id,MongoDB 会自动产生一个 ObjectId 类型的 _id 属性。insert() 方法会在执行后返回的 _id 值:
5932a68615c2606814c91f3d
'''
#【插入多个字典数据,用列表把字典组合】:
student1 = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
student2 = {
'id': '',
'name': 'Mike',
'age': 21,
'gender': 'male'
}
result = collection.insert([student1, student2])
print(result)
'''
返回的结果是对应的 _id 的集合,运行结果:
[ObjectId('5932a80115c2606a59e8a048'), ObjectId('5932a80115c2606a59e8a049')]
'''
三、【查询一】用 find_one() 或 find() 方法进行查询,find_one() 查询得到是单个结果,find() 则返回一个生成器对象。
result = collection.find_one({'name': 'Mike'})
print(type(result))
print(result)
'''
在这里我们查询 name 为 Mike 的数据,它的返回结果是字典类型,运行结果:<class 'dict'>
{'_id': ObjectId('5932a80115c2606a59e8a049'), 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}
可以发现它多了一个 _id 属性,这就是 MongoDB 在插入的过程中自动添加的。
'''
三、【查询二】单条数据查询
from bson.objectid import ObjectId
result = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')})
print(result)
'''
其查询结果依然是字典类型,运行结果:
{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}'''
三、【查询三】多条数据的查询,我们可以使用 find() 方法,例如在这里查找年龄为 20 的数据,示例如下:
#
results = collection.find({'age': 20})
print(results)
for result in results:
print(result)
'''
运行结果:
<pymongo.cursor.Cursor object at 0x1032d5128>
{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('593278c815c2602678bb2b8d'), 'id': '20170102', 'name': 'Kevin', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('593278d815c260269d7645a8'), 'id': '20170103', 'name': 'Harden', 'age': 20, 'gender': 'male'}
'''
如果要查询年龄大于 20 的数据,则写法如下:
results = collection.find({'age': {'$gt': 20}})