mongodb 修改器($inc/$set/$unset/$push/$pop/upsert))   https://www.jb51.net/article/112588.htm
http://blog.csdn.net/yaomingyang/article/details/78701643

一、$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"

 db.stores.update( { }, { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }, { multi: true } )

操作后的结果是:

{
"_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))的更多相关文章

  1. Mongodb更新数组$pull修饰符

    http://blog.csdn.net/yaomingyang/article/details/78701643 一.$pull修饰符会删除掉数组中符合条件的元素,使用的格式是: { $pull:  ...

  2. MongoDB-比较符及修改器

    数学比较符 $lt 小于 $lte 小于等于 $gt 大于 $gte 大于等于 $eq 等于 $ne 不等于 所有数据 > db.stutent.find() }) { "_id&qu ...

  3. mongodb修改器

    mongodb修改器 转载自:http://blog.csdn.net/mcpang/article/details/7752736 mongodb修改器(\(inc/\)set/\(unset/\) ...

  4. [转]Java反射之如何判断类或变量、方法的修饰符(Modifier解析)

    Java针对类.成员变量.方法,有很多修饰符,例如public.private.static.final.synchronized.abstract等,这些修饰符用来控制访问权限或其他特性. 本文就用 ...

  5. override 修饰符

    override(C# 参考) 要扩展或修改继承的方法.属性.索引器或事件的抽象实现或虚实现,必须使用 override 修饰符. C# abstract class ShapesClass { ab ...

  6. C语言scanf函数转换说明表及其修饰符表

    1. 对于上一篇文章,总结printf()输出,C库也包含了多个输入函数, scanf()是最常用的一个,也是经常与printf()经常一起搭配使用的函数之一. scanf()和printf()类似, ...

  7. 概述C# virtual修饰符

    摘要:C#是继C++和Java语言后的又一面向对象的语言,在语法结构,C#有很多地方和C++及Java相似,但是又不同于它们,其中一些关键特别需要引起我们的注意. C# virtual修饰符用于修改方 ...

  8. MongoDB学习day08--mongoose预定义修饰符和getter、setter修饰符

    一.mongoose预定义修饰符 lowercase. uppercase . trim var UserSchema=mongoose.Schema({ name:{ type:String, tr ...

  9. mongodb的修改器

    在mongodb中通常文档只会有一部分要更新,利用原子的更新修改器,可以做到只更新文档的一部分键值,而且更新极为高效,更新修改器是种特殊的键,用来指定复杂的更新操作,比如调整.增加.或者删除键,还可以 ...

随机推荐

  1. 【轻松一刻】Java制作字符动画

    前言 今晚闲来无事,整理了一下电脑中尘封已久的旧代码,看着那些年自己写过的代码,踩过的坑,顿时老泪纵横.正当在感叹之际,突然发现在“马克思”文件夹下出现了一个好玩的项目,那就是N年前刚学Java时写的 ...

  2. Echarts配置项详解

    1.图表标题 title: { x: 'left', // 水平安放位置,默认为左对齐,可选为: // 'center' ¦ 'left' ¦ 'right' // ¦ {number}(x坐标,单位 ...

  3. SpringBoot mysql出现The server time zone value '�й���׼ʱ��' is unrecogni

    MySql :8.0.18 引入的mysql驱动: SpringBoot整合Mybatis的框架,在访问Controller的时候 : ava.sql.SQLException: The server ...

  4. PHP程序功能设计

    以留言板为例. 数据表设计 分析数据表结构:有哪些信息需要存储:留言信息:ID,留言标题,留言内容,留言时间,留言人 CREATE TABLE message( id INT UNSIGNED NOT ...

  5. ajax _flask

    同步访问 当客户端向服务器发送请求时,服务器在处理过程中,浏览器只能等等,效率偏低 异步访问: 当客户端向服务器发送请求时,服务器在处理过程中,客户端可以做其他的操作,不需要一直等待,效率偏高 AJA ...

  6. 04 Windows编程——Unicode

    VS 2017下源码 #include<stdio.h> int main() { char ASC_a = 'a'; char *ASC_str = "hello"; ...

  7. Qt一些方便易用的小技巧

    延迟给自己发信号执行操作 //延迟4500毫秒, 改变Status的值. QTimer::singleShot(4500, this, [&](){ this->Status = 0; ...

  8. KubeEdge v0.2发布,全球首个K8S原生的边缘计算平台开放云端代码

    KubeEdge开源背景 KubeEdge在18年11月24日的上海KubeCon上宣布开源,技术圈曾掀起一阵讨论边缘计算的风潮,从此翻开了边缘计算和云计算联动的新篇章. KubeEdge即Kube+ ...

  9. PAT Advanced 1073 Scientific Notation (20 分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  10. HDU 6568 Math

    Math \(f_i\)为从\(i\)到\(i+1\)的期望步数. \(f_i = 1-p + p(f_i + 2((1-q)^{n-i}(n-i) + q\sum_{j=0}^{n-i-1}(1-q ...