首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

pymongo参考文档

零、参考资料

官方文档

https://pymongo.readthedocs.io/en/stable/

本文涉及的代码已经上传至我的gitee中

https://gitee.com/yz6zy/pymongo_study

一、使用方法

1.1 数据库连接

from pymongo import MongoClient # 默认主机和端口 # client = MongoClient() # 显示指定 try: client = MongoClient("localhost", 27017) print("连接成功") except Exception as e: print(f"连接失败,错误原因{e}")

将这个方法封装成函数,用于以下内容的使用

from pymongo import MongoClient # 默认主机和端口 # client = MongoClient() # 显示指定 def connect(): try: client = MongoClient("localhost", 27017) print("连接成功") return client except Exception as e: print(f"连接失败,错误原因{e}")

1.2 使用数据库

import connect client = connect.connect() # 使用数据库test db = client['test'] # 获取对应的集合(列表) collection = db.test

1.3 查询某个集合的数据

find_one(condition):按条件查询一条数据

find_many(condition):按条件查询多条数据

import connect client = connect.connect() # 使用数据库test db = client['test'] # 获取对应的集合(列表) collection = db.test # 展示列表的一个内容 data = collection.find_one() # 得到dict print(data) # 展示列表的所有内容 result = collection.find() # 得到 print(type(result)) for item in result: # 每个内容是个单独的dict print(item)

1.4 插入数据

insert_one(condition):按条件插入一条数据

insert_many(condition):按条件插入多条数据

import connect client = connect.connect() # 使用数据库test db = client['test'] # 获取对应的集合(列表) collection = db.test # 展示列表的所有内容 def showCollectionData(c): result = c.find() for item in result: print(item) # 单个插入 # insert_data = { # "key": "123", # "age": 18, # "name": "john" # } # collection.insert_one(insert_data) # showCollectionData(collection) # 批量插入 insert_data = [ { "key": "123", "age": 18, "name": "john" }, { "key": "456", "age": 20, "name": "lucy" }, { "key": "789", "age": 22, "name": "mike" }, ] collection.insert_many(insert_data) showCollectionData(collection)

1.5 对集合数据条数计数

import connect client = connect.connect() # 使用数据库test db = client['test'] # 获取对应的集合(列表) collection = db.test # 查看当前的文档有多少条数据 cur_count1 = collection.count_documents({}) print(cur_count1) # 根据条件匹配对应的数据 # 这里匹配key为123的数据 cur_count2 = collection.count_documents({"key": "123"}) print(cur_count2)

1.6 条件查询

$gt:大于等于

$lt:小于等于

import connect # 高级查询import connect client = connect.connect() # 使用数据库test db = client['test'] # 获取对应的集合(列表) collection = db.test # 插入数据 insert_data = [ { "key": "111", "age": 18, "name": "john" }, { "key": "222", "age": 20, "name": "lucy" }, { "key": "333", "age": 22, "name": "mike" }, ] collection.insert_many(insert_data) # 我们需要查找年龄 19

1.7 更新数据

update_one(condition):按条件更新一条数据

update_many(condition):按条件更新多条数据

$set:表示直接更换数据

$inc:表示在原有的数据上增加,如果要减少直接给负数就行

import connect client = connect.connect()

# 使用数据库

test db = client['test']

# 展示列表的所有内容

def showCollectionData(c):

result = c.find() for item in result: print(item)

# 获取对应的集合(列表)

collection = db.test

# 展示内容 showCollectionData(collection)

# 更新key为test的,把key改成test666

# $set 不存在则创建,存在则直接替换

# collection.update_one({"key":"test"}, { "$set": {"key": "test666"} })

# showCollectionData(collection)

# 增加, 将年龄为22的数据,增加一个height数据,值为180 collection.update_one({"age": 22}, { "$set": {"height": 180} }) showCollectionData(collection) # 更新多个

collection.update_many({"age": 22}, { "$set": {"height": 180} }) showCollectionData(collection)

1.8 删除数据

delete_one(condition):按条件删除一条数据

delete_many(condition):按条件删除多条数据

import connect client = connect.connect() # 使用数据库test db = client['test'] # 获取对应的集合(列表) collection = db.test # 展示列表的所有内容 def showCollectionData(c): result = c.find() for item in result: print(item) showCollectionData(collection) # 删除一条key为test2 # collection.delete_one({ "key": "test2" }) # showCollectionData(collection) # 删除多条age为18的数据 collection.delete_many({ "age": 18 }) showCollectionData(collection)

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OsvxhMNwC-PCRu0poHCBAFuQ0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券