Python - Pymongo

windows 安装mongodb

下载地址:

https://www.mongodb.com/try/download/community

tips:

  • 不下载这个图形化工具,可能会很慢:

  • 配置环境变量到Path

E:\mongodb\bin
  • 相关命令,需要以管理员身份执行:
# 启动服务
net start mongodb
# 关闭服务
net stop mongodb
  • 检查服务:

图形连接工具下载地址:

https://robomongo.org/

连接成功后创建数据库:

在库的基础上创建collection,相当于表的概念

pymongo 包的学习使用

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)

db = client.test # 获得数据库
collection = db.students  # 获得connection
# 准备插入的数据
student = {
    'id': '20170101',
    'name': 'Jondon',
    'age': 20,
    'gender': 'male'
}

result = collection.insert_one(student)

print(result)  #out:<pymongo.results.InsertOneResult object at 0x000002465B5286A0>
print(result.inserted_id) #out: 6381e8900ef9def244c4fdb6



# 插入多条数据

student1 = {
    'id': '20170101',
    'name': 'Jondon1',
    'age': 20,
    'gender': 'male'
}

student2 = {
    'id': '20170101',
    'name': 'Jondon2',
    'age': 20,
    'gender': 'male'
}

result = collection.insert_many([student1,student2])

print(result) #out:<pymongo.results.InsertManyResult object at 0x0000018EAADA8A60>
print(result.inserted_ids) #out: [ObjectId('6381e905793fe7c2a73fc6e3'), ObjectId('6381e905793fe7c2a73fc6e4')]


import pymongo
client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test # 获得数据库
collection = db.students  # 获得connection

# 查询一条数据
result = collection.find_one({'name':'Jondon2'}) #out:{'_id': ObjectId('6381e8feb6f884f0223e9cdb'), 'id': '20170101', 'name': 'Jondon2', 'age': 20, 'gender': 'male'}
print(result)
print(type(result)) #out:<class 'dict'>


# 查询多条数据
result = collection.find({'name':'Jondon2'})
print(result)
print(type(result))
for item in result:
    print(item)


'''
<pymongo.cursor.Cursor object at 0x000001A3170F76A0>
<class 'pymongo.cursor.Cursor'>
{'_id': ObjectId('6381e8feb6f884f0223e9cdb'), 'id': '20170101', 'name': 'Jondon2', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('6381e905793fe7c2a73fc6e4'), 'id': '20170101', 'name': 'Jondon2', 'age': 20, 'gender': 'male'}
'''



# 查询年龄大于20的数据:
result = collection.find({'age':{'$gt':20}}) 
print(list(result)) # out:[{'_id': ObjectId('6381ed245c197324db7dd8ff'), 'id': '20170101', 'name': 'Jondon1', 'age': 21, 'gender': 'male'}]

# 正则表达式匹配
# 匹配name 以J开头的学生
result = collection.find({'name':{'$regex':'^J.*'}})


#排序:将查询结果按name升序排序
results = collection.find().sort('name', pymongo.ASCENDING)  
a = [result['name'] for result in results]
print(a)  # out: ['Aondon', 'Condon', 'Eondon', 'Iondon1', 'Jondon', 'Pondon2', 'Rondon1', 'Uondon2', 'Wondon', 'Yondon2', 'Zondon1']


# 偏移
results = collection.find().sort('name', pymongo.ASCENDING).skip(2)
print([result['name'] for result in results]) # out:['Eondon', 'Iondon1', 'Jondon', 'Pondon2', 'Rondon1', 'Uondon2', 'Wondon', 'Yondon2', 'Zondon1']


# limit 限制结果数量
results = collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)
print([result['name'] for result in results]) # out:['Eondon', 'Iondon1']

其他比较运算符参考:

https://www.jianshu.com/p/d0b23b16e46a

import pymongo
client = pymongo.MongoClient(host='localhost', port=27017)

db = client.test # 获得数据库
collection = db.students  # 获得connection

codition = {'name': 'Zondon1'}
student = collection.find_one(codition) # age 20
student['age'] = 28 # 更新为28
result = collection.update_one(codition, {'$set':student})
print(result.matched_count,result.modified_count) # 分别返回匹配到的行数和更改的行数


# 更改多条数据
condition = {'age': {'$gt':20}}
student  = collection.find_one(condition)
result = collection.update_many(condition,{'$inc':{'age': 1}})  # 将大于20岁的学生的你年纪加1
print(result.matched_count,result.modified_count)
posted @ 2022-11-26 17:34  chuangzhou  阅读(31)  评论(0编辑  收藏  举报