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': {

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

被折叠的 条评论
为什么被折叠?



