http://blog.csdn.net/yaomingyang/article/details/78701643

一、$pull修饰符会删除掉数组中符合条件的元素,使用的格式是:

  1. { $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

二、指定一个值删除所有的列表

给一个stores集合下的文档

  1. {
  2. _id: 1,
  3. fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
  4. vegetables: [ "carrots", "celery", "squash", "carrots" ]
  5. }
  6. {
  7. _id: 2,
  8. fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
  9. vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
  10. }

如下操作更新所有的文档在集合stores中的"apples"和"oranges"在数组fruits中和删除数组vegetables中的"carrots"

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

操作后的结果是:

  1. {
  2. "_id" : 1,
  3. "fruits" : [ "pears", "grapes", "bananas" ],
  4. "vegetables" : [ "celery", "squash" ]
  5. }
  6. {
  7. "_id" : 2,
  8. "fruits" : [ "plums", "kiwis", "bananas" ],
  9. "vegetables" : [ "broccoli", "zucchini", "onions" ]
  10. }

三、$pull删除所有符合条件的元素

根据集合profiles集合文档

  1. { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }

如下操作会删除掉votes数组中元素大于等于6的元素

  1. db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

操作 之后数组中都是小于6的元素

  1. { _id: 1, votes: [  3,  5 ] }

四、从一个数组嵌套文档中删除元素

一个survey集合包含如下文档

  1. {
  2. _id: 1,
  3. results: [
  4. { item: "A", score: 5 },
  5. { item: "B", score: 8, comment: "Strongly agree" }
  6. ]
  7. }
  8. {
  9. _id: 2,
  10. results: [
  11. { item: "C", score: 8, comment: "Strongly agree" },
  12. { item: "B", score: 4 }
  13. ]
  14. }

如下操作将会删除掉数组results中元素item等于B、元素score等于8的文档集合

  1. db.survey.update(
  2. { },
  3. { $pull: { results: { score: 8 , item: "B" } } },
  4. { multi: true }
  5. )

操作后的结果是:

  1. {
  2. "_id" : 1,
  3. "results" : [ { "item" : "A", "score" : 5 } ]
  4. }
  5. {
  6. "_id" : 2,
  7. "results" : [
  8. { "item" : "C", "score" : 8, "comment" : "Strongly agree" },
  9. { "item" : "B", "score" : 4 }
  10. ]
  11. }

五、如下集合文档是数组套数组类型

  1. {
  2. _id: 1,
  3. results: [
  4. { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
  5. { item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] }
  6. ]
  7. }
  8. {
  9. _id: 2,
  10. results: [
  11. { item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] },
  12. { item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] }
  13. ]
  14. }

可以使用$elemMatch匹配多个条件

  1. db.survey.update(
  2. { },
  3. { $pull: { results: { answers: { $elemMatch: { q: 2, a: { $gte: 8 } } } } } },
  4. { multi: true }
  5. )

操作后的结果是:

  1. {
  2. "_id" : 1,
  3. "results" : [
  4. { "item" : "A", "score" : 5, "answers" : [ { "q" : 1, "a" : 4 }, { "q" : 2, "a" : 6 } ] }
  5. ]
  6. }
  7. {
  8. "_id" : 2,
  9. "results" : [
  10. { "item" : "C", "score" : 8, "answers" : [ { "q" : 1, "a" : 8 }, { "q" : 2, "a" : 7 } ] }
  11. ]
  12. }

Mongodb更新数组$pull修饰符的更多相关文章

  1. Mongodb更新数组$pull修饰符 (mongodb 修改器($inc/$set/$unset/$push/$pop/upsert))

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

  2. iOS 属性修饰符记录 --不定时更新

    重新审视了一下OC在属性修饰符,特意记录一下来.以后不定时更新 > retain:只有在非ARC下才会有效,所有如果在ARC下使用了retain修饰也白搭 如以下的data属性用retain修饰 ...

  3. vue中.sync修饰符,实现子组件实时更新父组件的值

    vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定. 不过它有一个前身,先来看看.sync出现之前是如何实现的 父组件中(传递给子组件一个值:p ...

  4. C语言回顾-整型变量修饰符和一维数组

    1.整型变量修饰符 1)改变整型变量的存储空间 #include <stdio.h> int main(int argc, const char * argv[]) { //改变整型变量占 ...

  5. EffectiveC#02--仅在对基类进行强制更新时才使用new修饰符

    1.建议避免使用new修饰符来重新定义非虚函数. 非虚方法是静态绑定的,不管哪里的代码也不管在哪里引用, 它总是严格的调用类中所定义的函数.并不会在运行时在 派生类中查找不同的版本. 2.何时使用ne ...

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

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

  7. C#基础回顾(一)—C#访问修饰符

    一.写在前面的话 好久没有停下来总结自己,转眼间15年过去好些天,回首过去的日子,亦或失去,亦或所得!生活的节奏,常常让我们带着急急忙忙的节奏去追赶,也许这并不是每个人所期望的生活方式!于他人,于自己 ...

  8. Vue.js学习笔记(三) - 修饰符

    本篇将简单介绍常用的修饰符. 在上一篇中,介绍了 v-model 和 v-on 简单用法.除了常规用法,这些指令也支持特殊方式绑定方法,以修饰符的方式实现.通常都是在指令后面用小数点“.”连接修饰符名 ...

  9. ch3-模板语法({{}} v-html v-bind:id 表达式 指令 修饰符 过滤器)

    1 模板语法 Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. 所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器 ...

随机推荐

  1. Hiho----无间道之并查集

    题目: 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之,小Hi和小Ho决定趁着这朗朗春光出去 ...

  2. HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. Android 垃圾回收,用软引用建立缓存

    内存对于手机来说是非常重要的. 下面总结了我们在注意创建对象时的规则,以及怎么更好更快的实行GC回收,和怎么构建高速的对象cace缓冲. 1 避免循环遍历的创建对象,哪怕对象很小,也是要占资源的. 2 ...

  4. 如何在form初始化时自动隐藏FOLDER列

    方法1:直接设定PROMPT列和数据列ITEM的VISIBLE属性为No 方法2:在WHEN-NEW-FORM-INSTANCE触发器里: l_old_itm := :system.cursor_it ...

  5. ylbtech-LanguageSamples-Libraries(库)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Libraries(库) 1.A,示例(Sample) 返回顶部 “库”示例 本示例演示 ...

  6. WAF防御能力评测及工具

    本篇文章介绍如何从常规攻击的防御能力来评测一款WAF.一共覆盖了十六种攻击类型,每种类型均从利用场景(攻击操作的目的),注入点(漏洞产生的地方,比如说大多数WAF都会较全面地覆盖来自GET请求的攻击, ...

  7. phpcms v9 wap手机门户站点内容页添加上一篇、下一篇的方法

    PHP源码修改:打开 phpcms\modules\wap\index.php 文件找到if(!$r || $r['status'] != 99) showmessage(L('info_does_n ...

  8. 【读书笔记】iOS-GCD-block-后台执行

    当一个app按home键退出的时候.仅仅有最多5秒的时间做一些保存或清理资源的工作. 可是调用beginBackgroundTaskWithExpirationHandler方法,能够最多有10分时间 ...

  9. JQuery的get、post和ajax方法的使用

    在JQuery中可以使用get,post和ajax方法给服务器端传递数据 get方法的使用(customForGet.js文件): function verify(){ //1.获取文本框的数据 // ...

  10. [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular

    Communicating with a remote server via HTTP presents an extra level of complexity as there is an inc ...