import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017/')#连接数据库
db = client.test #等价于client['test'],创建test数据库
#指定集合
collection = db.students #等价于db['students']
#插入数据
student = {
"id":"20170101",
"name":"Jorden",
"age":20,
"gender":"male"
}
# result = collection.insert_one(student)
# # result = collection.insert_many([student1,student2])
# # print(result.inserted_ids)
# print(result.inserted_id)
#查询
# Query_result = collection.find_one({'name':'Jorden'})
# print(type(Query_result))
# print(Query_result)
# <class 'dict'>
# {'_id': ObjectId('623dbe2bcfbd0be74da88977'), 'id': '20170101', 'name': 'Jorden', 'age': 20, 'gender': 'male'}
#查询多条数据用find(),返回生成器对象,结果遍历
# Query_result = collection.find({'age':{
pymongo 基础操作
最新推荐文章于 2023-03-06 15:05:43 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)