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

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

 

[plain] view plain copy
 
  1. { $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }  

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

 

给一个stores集合下的文档

 

[plain] view plain copy
 
  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"
[plain] view plain copy
 
  1. db.stores.update(  
  2.     { },  
  3.     { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } },  
  4.     { multi: true }  
  5. )  

操作后的结果是:

 

 

[plain] view plain copy
 
  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集合文档

 

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

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

 

 

[plain] view plain copy
 
  1. db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )  

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

 

 

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

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

 

一个survey集合包含如下文档

 

[plain] view plain copy
 
  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的文档集合

 

 

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

操作后的结果是:

 

 

[plain] view plain copy
 
  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. }  

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

 

 

[plain] view plain copy
 
  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匹配多个条件

 

 

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

操作后的结果是:

 

 

[plain] view plain copy
 
  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. }  
 posted on 2017-12-04 23:50  jayruan  阅读(11210)  评论(0编辑  收藏  举报