pymongo学习

本文介绍MongoDB中各种更新操作及条件查询方法,包括使用更新修改器如$set、$unset等对文档进行部分更新,利用$in、$or等操作符进行复合条件查询,以及数组操作符如$all、$size等对数组类型的数据进行精确匹配。

1. pymongo官方文档学习

学习官方文档,中文版本,快速上手

2. 学习资料

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

一些简单的操作http://www.zzvips.com/article/187670.html

3. 操作指南

更新操作update

在实际中,更新文档往往是更新文档的一部分内容,在 MongoDB 中,我们可以使用更新修改器 (update modifier) 来对文档中某些字段进行更新,常用的修改器有以下几个:

  • $set用来指定一个字段的值,如果不存在,将创建一个新的字段
  • $unset删除一个字段
  • $inc用来增加(或减少)一个已有键的值,如果不存在将会创建一个
  • $push 向已有的数组末尾添加一个元素
  • $addToSet避免插入重复数据
  • $pull删除元素,基于特定条件
  • $each遍历列表操作
  • $pop删除元素

$set

>> document = {
   
   'name': 'joe', 
               'age': 30,
               'sex': 'male',
               'location': 'Wisconsin' }

>> table.insert_one(document)

>> print(user.find_one({
   
   '_id':'5ac9836829561f64220f6f9d'}))

>> {
   
   '_id': ObjectId('5ac9836829561f64220f6f9d'),
    'age': 30,
    'location': 'Wisconsin',
    'name': 'joe',
    'sex': 'male'}

# 为用户添加一项 favorite 的字段
>> table.update_one({
   
   '_id': ObjectId('5ac9836829561f64220f6f9d')}, {
   
   '$set' : {
   
   'favorite': 'War adn Peace'}})

>> print(table.find_one({
   
   '_id':'5ac9836829561f64220f6f9d'}))

>> {
   
   '_id': ObjectId('5ac9836829561f64220f6f9d'),
    'age': 30,
    'favorite': 'War adn Peace',
    'location': 'Wisconsin',
    'name': 'joe',
    'sex': 'male'}

# 将 favorite 字段的值修改为 Green Eggs and Ham
>> table.update_one({
   
   'name': 'joe'}, {
   
   '$set': {
   
   'favorite': 'Green Eggs and Ham'}})

>> print(user.find_one({
   
   '_id':'5ac9836829561f64220f6f9d'}))

>> {
   
   '_id': ObjectId('5ac9836829561f64220f6f9d'),
    'age': 30,
    'favorite': 'Green Eggs and Ham',
    'location': 'Wisconsin',
    'name': 'joe',
    'sex': 'male'}

# 将 favorite 字段的值替换为一个数组
>> table.update_one({
   
   'name': 'joe'}, {
   
   '$set': {
   
   'favorite': ["Cat's Cradle": , "Foundation Trilogy", "Ender's Game"]}})

>> print(user.find_one({
   
   '_id':'5ac9836829561f64220f6f9d'}))

>> {
   
   '_id': ObjectId('5ac9836829561f64220f6f9d'),
    'age': 30,
    'favorite': ["Cat's Cradle", 'Foundation Trilogy', "Ender's Game"],
    'location': 'Wisconsin',
    'name': 'joe',
    'sex': 'male'}

# 内嵌文档修改
>> blog = database.blog # 创建一个 blog 集合

>> posts = {
   
   'title': 'A Blog Post', 'content': '...', 'author': {
   
   'name': 'joe', 'email': 'joe@example.com'}} # 创建一个 posts 文档

>> blog.insert_one(posts)

>> blog.find_one({
   
   'title':'A Blog Post'})

>> {
   
   '_id': ObjectId('5ac98a0a29561f64220f6f9e'),
    'author': {
   
   'email': 'joe@example.com', 'name': 'joe'},
    'content': '...',
    'title': 'A Blog Post'}

# 将作者名称字段 name 的值修改为 joe schmoe
>> blog.update_one({
   
   'author.name': 'joe'}, {
   
   '$set': {
   
   'author.name': 'joe schmoe'}})
>> blog.find_one()
>> {
   
   '_id': ObjectId('5ac98a0a29561f64220f6f9e'),
    'author': {
   
   'email': 'joe@example.com', 'name': 'joe schmoe'},
    'content': '...',
    'title': 'A Blog Post'}

$unset

# 删除 table集合中 joe 的 favorite 字段
>> print(table.find_one({
   
   '_id':'5ac9836829561f64220f6f9d'}))

>> {
   
   '_id': ObjectId('5ac9836829561f64220f6f9d'),
    'age': 30,
    'favorite': ["Cat's Cradle", 'Foundation Trilogy', "Ender's Game"],
    'location': 'Wisconsin',
    'name': 'joe',
    'sex': 'male'}
>> table.update_one({
   
   'name': 'joe'}, {
   
   '$unset': {
   
   'favorite': 1}})

>> print(table.find_one({
   
   '_id':'5ac9836829561f64220f6f9d'}))

>> {
   
   '_id': ObjectId('5ac9836829561f64220f6f9d'),
    'age': 30,
    'location': 'Wisconsin',
    'name': 'joe',
    'sex': 'male'}

$inc

# 创建一个 games 的集合,并向集合中添加数据
>> games = test_database.games

>> games.insert_one({
   
   'game': 'pinball', 'user': 'joe'})

>> print(games.find_one('_id':'5ac9c55f29561f64220f6f9f'))

>> {
   
   '_id': ObjectId('5ac9c55f29561f64220f6f9f'), 'game': 'pinball', 'user': 'joe'}

# 增加一个分数字段 score
>> games.update_one({
   
   'game': 'pinball', 'user': 'joe'}, {
   
   '$inc': {
   
   'score': 50}})

>> print(games.find_one('_id':'5ac9c55f29561f64220f6f9f'))

>> {
   
   '_id': ObjectId('5ac9c55f29561f64220f6f9f'),
    'game': 'pinball',
    'score': 50,
    'user': 'joe'}

# 为 score 字段的值增加 5000
>> games.update_one({
   
   'game': 'pinball', 'user': 'joe'}, {
   
   '$inc': {
   
   'score': 5000}})

>> print(games.find_one('_id':'5ac9c55f29561f64220f6f9f'))

>> {
   
   '_id': ObjectId('5ac9c55f29561f64220f6f9f'),
    'game': 'pinball',
    'score': 5050,
    'user': 'joe'}

$push

# 选择 blog 数据库
>> blog = test_database.blog

>> print(blog.find_one('_id':'5ac98a0a29561f64220f6f9e'))

>> {
   
   '_id': ObjectId('5ac98a0a29561f64220f6f9e'),
    'author': {
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fighting_1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值