cbowen

【MongoDB详细使用教程】一、Mac安装MongoDB
【MongoDB详细使用教程】二、MongoDB基本操作
【MongoDB详细使用教程】三、高级查询
【MongoDB详细使用教程】四、python操作MongoDB
【MongoDB详细使用教程】五、MongoDB的数据库管理

使用第三方库pymongo来实现python对MongoDB的操作
pymongo官方文档:https://api.mongodb.com/python/current/tutorial.html

1、安装pymongo

pip install 安装pymongo

2、连接数据库

import pymongo

client = pymongo.MongoClient(\'localhost\', 27017)    # 连接服务器,需要先开启服务
db = client[\'mymongo\']  # 选择数据库
data = db.students.find()   # 查询数据,返回一个游标,通过对游标进行遍历来获取每条数据 
print(db)
print(data)


# 对查询到的数据进行遍历,每一项为一个dict
for i in data:
    print(i, type(i))

返回结果:

MongoClient(host=[\'localhost:27017\'], document_class=dict, tz_aware=False, connect=True)
Database(MongoClient(host=[\'localhost:27017\'], document_class=dict, tz_aware=False, connect=True), \'mymongo\')
<pymongo.cursor.Cursor object at 0x1058e57f0>
{\'_id\': ObjectId(\'5db642b30f98841018f76965\'), \'name\': \'chen\', \'age\': 18.0, \'grade\': \'一年级\'} <class \'dict\'>
{\'_id\': ObjectId(\'5db642bc0f98841018f76966\'), \'name\': \'wang\', \'age\': 19.0, \'grade\': \'二年级\'} <class \'dict\'>
{\'_id\': ObjectId(\'5db653920f98841018f7696b\'), \'name\': \'xu\', \'age\': 20.0, \'grade\': \'三年级\', \'text\': [\'女\', \'研究员\']} <class \'dict\'>
{\'_id\': ObjectId(\'5db654660f98841018f7696c\'), \'name\': \'ma\', \'age\': 20.0, \'grade\': \'二年级\', \'text\': [\'女\', \'副教授\', \'副处长\']} <class \'dict\'>
{\'_id\': ObjectId(\'5db68d190f98841018f76970\'), \'name\': \'cheng\', \'age\': 21.0, \'grade\': \'四年级\'} <class \'dict\'>
{\'_id\': ObjectId(\'5db68f6c0f98841018f76971\'), \'name\': \'cheng\', \'age\': 22.0, \'grade\': \'五年级\'} <class \'dict\'>

3、操作数据库

python操作mysql和oracle都是通过直接执行sql来完成,
而对MongoDB的操作是通过pymongo提供的方法来完成的。

本节不再把语法单独提出,所有"students"字样均为集合名。

3.1、查

data_all = db.students.find()   # 查询全部
data_lim = db.students.find().limit(1)  # 返回第一条
data_the = db.students.find({"name": "xu"})  # 条件查询(结果只有1条匹配)
data_one = db.students.find_one()   # 查询一条


print(data_all, type(data_all))
print(data_lim, type(data_lim))
print(data_the, type(data_the))
print(data_one, type(data_one))

虽然后3个方法得到的数据都是1条,但只有使用.find_one()时会返回dict,其余返回的都是Cursor(游标),需要遍历才可以得到具体数据。

<pymongo.cursor.Cursor object at 0x105a04780> <class \'pymongo.cursor.Cursor\'>
<pymongo.cursor.Cursor object at 0x105a047f0> <class \'pymongo.cursor.Cursor\'>
<pymongo.cursor.Cursor object at 0x105a04860> <class \'pymongo.cursor.Cursor\'>
{\'_id\': ObjectId(\'5db642b30f98841018f76965\'), \'name\': \'chen\', \'age\': 18.0, \'grade\': \'一年级\'} <class \'dict\'>

3.2、增

# 插入单条
db.students.insert_one({"name": "zuo", "age": 40, "grate": "九年级"})

# 插入多条
many_data = [{"name": "ding", "age": 40, "grate": "九年级"},
             {"name": "liao", "age": 42, "grate": "十年级"},
             {"name": "zhao", "age": 35, "grate": "九年级"}]
db.students.insert_many(many_data)

3.3、改

# 修改单条
db.students.update_one(filter={"name": "zuo"}, update={"$set": {"grate": "十年级"}})

# 修改全部匹配项
db.students.update_many(filter={"name": "zuo"}, update={"$set": {"grate": "十年级"}})


# filter后为条件,update后为修改后值,其中$set为固定语法。

3.4、删

# 删除单条
db.students.delete_one({})  # 删除全部数据的第一条
db.students.delete_one({"name": "zuo"}) # 删除匹配项的第一条

# 删除多条
db.students.delete_many({"name":"zuo"}) # 删除集合中的全部数据
db.students.delete_many({"name":"zuo"}) # 删除全部匹配项

分类:

技术点:

相关文章:

  • 2022-02-08
  • 2022-02-08
  • 2022-12-23
  • 2021-10-23
  • 2022-12-23
  • 2021-12-03
  • 2021-04-15
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2022-02-08
  • 2022-03-05
  • 2022-02-08
  • 2022-01-01
  • 2022-02-08
相关资源
相似解决方案