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. }

最新文章

  1. javascript escape()函数和unescape()函数
  2. GDUFE-OJ 1070上班打卡 ^位运算
  3. phpcms 中路径问题
  4. C# 通过反射类动态调用DLL方法
  5. Spring 事务注解 错误问题
  6. 使用finfo_file()函数检测上传图片的类型
  7. PHP 魔术方法总结
  8. UI进阶 CocoaPods的安装使用步骤
  9. sql中的触发器、视图、事务
  10. apache .htaccess 伪静态重定向,防盗链 限制下载...
  11. fork和缓冲区
  12. 初涉JavaScript模式 (13) : 代码复用 【上】
  13. [转] gdb 查看vector, list, map 内容
  14. java.util.zip - Recreating directory structure(转)
  15. (十) Jquery的基本使用
  16. 虽然不抱希望但也愿.Net和Java之争暂得平息
  17. CAS部署在Windows上
  18. 实现PHP服务端和c#客户端数据交换
  19. 解决关于 ionic3 启动白屏 控制台错误提示:Uncaught SyntaxError Use of const in strict mode.
  20. ES的Query、Filter、Metric、Bucketing使用详解

热门文章

  1. (Mark=转)ehcache memcache redis
  2. 判断 Selite中标存在或者字段存在的方法
  3. Vue 小项目的最佳实践
  4. 【java】java中替换中括号[ ]操作
  5. 区块链核心技术:拜占庭共识算法之PBFT
  6. 12.线程通信CyclicBarrier
  7. iOS: 使用故事板和xib设置按钮圆角方法
  8. Node &amp; Cheerio &amp; WebStorm 学习实验
  9. 内向者沟通圣经:4P法(Preparation,Presence,Push,Practice)
  10. needtrue需要真实的答案