Pymongo学习笔记

〇、准备环境

1. 本地安装MongoDB客户端;

2. 本地安装MongoDB Compass,是Mongo数据库的一个人机交互界面;

3. 以下为个人整理的 MongoDB 的文档结构,可以通过 MongoDB Compass 工具进一步深入了解。

client
    |
    database_0
    |    |
    |    collections_0,文档集合
    |    |    |
    |    |    documents_0,json 集合
    |    |    |    |
    |    |    |    json_0 存储的json文档所在,唯一 _id
    |    |    |    json_1
    |    |    |    json_2
    |    |    |    ...
    |    |    |
    |    |    documents_1
    |    |    documents_2
    |    |    ...
    |    |
    |    collections_1
    |    collections_2
    |    ...
    |
    database_1
    database_2
    ...

一、基础:插入数据

1. Python连接数据库客户端

import pymongo

# 默认参数为 'mongodb://localhost:27017/', 连接本地默认端口数据库
client = pymongo.MongoClient()

2. 使用或创建数据库、使用或创建文档集合

# 使用数据库 'blog_db', 如数据库不存在,则“创建它”
db = client['blog_db']

# 使用集合 'blog_collection',如集合不存在,则“创建它”
collection = db['blog_collection']

3. 向数据库集合中添加一条 JSON 数据

blog_json = {
        'id':1234567890,
        'date': '2022年5月2日',
        'time': '16点42分',
        'location': '江苏省',
        'TabsInBrowser': {
            'first': 'bing',
            'second': 'csdn',
            'third': 'baidu',
            'fourth': 'eo',
            'number': 4
        },
        'mood': 'happy'
    }

# 如数据库 'blog_db' 或集合 'blog_collection' 不存在,则 insert_one 时会创建他们
collection.insert_one(blog_json)

 4. 查看本地MongoDB在执行前状态,运行 /bin/mongo.exe,执行命令 show dbs

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mech    0.000GB
test    0.000GB
wbc     0.001GB

运行前述步骤代码之后:

> show dbs                    | 命令作用:显示所有数据库
admin    0.000GB        
blog_db  0.000GB
config   0.000GB
local    0.000GB
mech     0.000GB
test     0.000GB
wbc      0.001GB
> use blog_db                 | 命令作用:使用数据库 'blog_db'
switched to db blog_db
> db.getCollectionNames()     | 命令作用:列出所有集合名称
[ "blog_collection" ]
>

在 MongoDB Compass 中查看:

 5. 意外的错误使用:为集合后缀,collection.sub_name

如果在第 3 步骤中,将

collection.insert_one(blog_json)

替换为

collection['sub_name'].insert_one(blog_json)

即将单个的集合 collection 名称被添加后缀,则所得结果为:

> show dbs
admin    0.000GB
blog_db  0.000GB
config   0.000GB
local    0.000GB
mech     0.000GB
test     0.000GB
wbc      0.001GB
> use blog_db
switched to db blog_db
> db.getCollectionNames()
[ "blog_collection.sub_name" ]
>

在 MongoDB Compass 中如下图所示

 至于该命令的如此设计的原因及原理,尚未学习到。

6. 避免插入重复数据

使用对集合层级的查询函数 find_one 查看待插入元素是否已经存在。例如查看关键 id 是否已经存在:

find_id  = { 'id':1234567890 }

"""
对 collection 层级操作的 find_one、find 使用相同的函数参数,区别在于后者是一个迭代器 Cursor
class Cursor: A cursor / iterator over Mongo query results."""
前者是获取到的第一个 JSON 文档
"""
result = collection.find_one(filter = find_id)
if not result:
    collection['sub_name'].insert_one(blog_json)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值