Mongodb更新数组$pull修饰符 (mongodb 修改器($inc/$set/$unset/$push/$pop/upsert))
mongodb 修改器($inc/$set/$unset/$push/$pop/upsert)) https://www.jb51.net/article/112588.htm
一、$pull修饰符会删除掉数组中符合条件的元素,使用的格式是:
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
二、指定一个值删除所有的列表
给一个stores集合下的文档
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
}
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
如下操作更新所有的文档在集合stores中的"apples"和
"oranges"在数组
fruits
中和删除数组
vegetables
中的"carrots"
操作后的结果是:
{
"_id" : 1,
"fruits" : [ "pears", "grapes", "bananas" ],
"vegetables" : [ "celery", "squash" ]
}
{
"_id" : 2,
"fruits" : [ "plums", "kiwis", "bananas" ],
"vegetables" : [ "broccoli", "zucchini", "onions" ]
}
三、$pull删除所有符合条件的元素
根据集合profiles集合文档
{ _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }
如下操作会删除掉votes数组中元素大于等于6的元素
db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )
操作 之后数组中都是小于6的元素
{ _id: 1, votes: [ 3, 5 ] }
四、从一个数组嵌套文档中删除元素
一个survey集合包含如下文档
{
_id: 1,
results: [
{ item: "A", score: 5 },
{ item: "B", score: 8, comment: "Strongly agree" }
]
}
{
_id: 2,
results: [
{ item: "C", score: 8, comment: "Strongly agree" },
{ item: "B", score: 4 }
]
}
如下操作将会删除掉数组results中元素item等于B、元素score等于8的文档集合
db.survey.update(
{ },
{ $pull: { results: { score: 8 , item: "B" } } },
{ multi: true }
)
操作后的结果是:
{
"_id" : 1,
"results" : [ { "item" : "A", "score" : 5 } ]
}
{
"_id" : 2,
"results" : [
{ "item" : "C", "score" : 8, "comment" : "Strongly agree" },
{ "item" : "B", "score" : 4 }
]
}
五、如下集合文档是数组套数组类型
{
_id: 1,
results: [
{ item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
{ item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] }
]
}
{
_id: 2,
results: [
{ item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] },
{ item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] }
]
}
可以使用$elemMatch匹配多个条件
db.survey.update(
{ },
{ $pull: { results: { answers: { $elemMatch: { q: 2, a: { $gte: 8 } } } } } },
{ multi: true }
)
操作后的结果是:
{
"_id" : 1,
"results" : [
{ "item" : "A", "score" : 5, "answers" : [ { "q" : 1, "a" : 4 }, { "q" : 2, "a" : 6 } ] }
]
}
{
"_id" : 2,
"results" : [
{ "item" : "C", "score" : 8, "answers" : [ { "q" : 1, "a" : 8 }, { "q" : 2, "a" : 7 } ] }
]
}
作者:随风yy
来源:CSDN
原文:https://blog.csdn.net/yaomingyang/article/details/78701643
版权声明:本文为博主原创文章,转载请附上博文链接!
Mongodb更新数组$pull修饰符 (mongodb 修改器($inc/$set/$unset/$push/$pop/upsert))的更多相关文章
- Mongodb更新数组$pull修饰符
http://blog.csdn.net/yaomingyang/article/details/78701643 一.$pull修饰符会删除掉数组中符合条件的元素,使用的格式是: { $pull: ...
- MongoDB-比较符及修改器
数学比较符 $lt 小于 $lte 小于等于 $gt 大于 $gte 大于等于 $eq 等于 $ne 不等于 所有数据 > db.stutent.find() }) { "_id&qu ...
- mongodb修改器
mongodb修改器 转载自:http://blog.csdn.net/mcpang/article/details/7752736 mongodb修改器(\(inc/\)set/\(unset/\) ...
- [转]Java反射之如何判断类或变量、方法的修饰符(Modifier解析)
Java针对类.成员变量.方法,有很多修饰符,例如public.private.static.final.synchronized.abstract等,这些修饰符用来控制访问权限或其他特性. 本文就用 ...
- override 修饰符
override(C# 参考) 要扩展或修改继承的方法.属性.索引器或事件的抽象实现或虚实现,必须使用 override 修饰符. C# abstract class ShapesClass { ab ...
- C语言scanf函数转换说明表及其修饰符表
1. 对于上一篇文章,总结printf()输出,C库也包含了多个输入函数, scanf()是最常用的一个,也是经常与printf()经常一起搭配使用的函数之一. scanf()和printf()类似, ...
- 概述C# virtual修饰符
摘要:C#是继C++和Java语言后的又一面向对象的语言,在语法结构,C#有很多地方和C++及Java相似,但是又不同于它们,其中一些关键特别需要引起我们的注意. C# virtual修饰符用于修改方 ...
- MongoDB学习day08--mongoose预定义修饰符和getter、setter修饰符
一.mongoose预定义修饰符 lowercase. uppercase . trim var UserSchema=mongoose.Schema({ name:{ type:String, tr ...
- mongodb的修改器
在mongodb中通常文档只会有一部分要更新,利用原子的更新修改器,可以做到只更新文档的一部分键值,而且更新极为高效,更新修改器是种特殊的键,用来指定复杂的更新操作,比如调整.增加.或者删除键,还可以 ...
随机推荐
- mini.DataGrid使用说明
mini.DataGrid表格.实现分页加载.自定义列.单元格渲染.行编辑器.锁定列.过滤行.汇总行等功能.Extend mini.PanelUsage <div id="dat ...
- VBA宏注释(四)
注释用于记录程序逻辑和用户信息,其他程序员将来可以阅读并理解相同的代码无缝工作. 它包括由开发者,修改者以及还可以包括合并逻辑的信息. 解释器在执行时忽略注释. VBA中的注释用两种方法表示,它们分别 ...
- list通过lambda 表达式去重,筛选
List<User> distinctList = new ArrayList();User user1 = new User();user1.setId("111") ...
- mysql 创建用户,授权,查询用户等
MySQL创建用户与授权 一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用 ...
- window service 批处理安装/卸载命令
开启服务 @echo.服务启动...... @echo off @sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32sr ...
- moment——日期格式化常用示例
<template> <div id="app" style="text-align: center;"> <h1>{{ m ...
- Linux shell批量执行scp脚本工具
转载: linux shell + expect:批量scp脚本工具 2011-09-13 15:51:06 分类: Python/Ruby 最近在准备一个部署的任务,其中有一 ...
- C++---初识《通过g++ / makefile 编译和调用动态库so文件》(ubuntu)
C++---初识<通过g++ / makefile 编译和调用动态库so文件>(ubuntu) ------------------------目录------------------- ...
- Ubuntu系统---C++之VScode IDE 编译器安装
Ubuntu系统---C++之VScode IDE 编译器安装 简单了解了一下VScode,直观印象:安装包很小(不像VS那么大占用十G左右).跨平台.小巧.可以编译C++ / java / pyth ...
- 使用python批量造测试数据
# -*- coding:utf-8 -*- import json import os import time class Virtual_Data: def __init__(self): sel ...