Mongodb更新数组$pull修饰符
一、$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 } ] }
- ]
- }
Mongodb更新数组$pull修饰符的更多相关文章
- 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 ...
- iOS 属性修饰符记录 --不定时更新
重新审视了一下OC在属性修饰符,特意记录一下来.以后不定时更新 > retain:只有在非ARC下才会有效,所有如果在ARC下使用了retain修饰也白搭 如以下的data属性用retain修饰 ...
- vue中.sync修饰符,实现子组件实时更新父组件的值
vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定. 不过它有一个前身,先来看看.sync出现之前是如何实现的 父组件中(传递给子组件一个值:p ...
- C语言回顾-整型变量修饰符和一维数组
1.整型变量修饰符 1)改变整型变量的存储空间 #include <stdio.h> int main(int argc, const char * argv[]) { //改变整型变量占 ...
- EffectiveC#02--仅在对基类进行强制更新时才使用new修饰符
1.建议避免使用new修饰符来重新定义非虚函数. 非虚方法是静态绑定的,不管哪里的代码也不管在哪里引用, 它总是严格的调用类中所定义的函数.并不会在运行时在 派生类中查找不同的版本. 2.何时使用ne ...
- MongoDB学习day08--mongoose预定义修饰符和getter、setter修饰符
一.mongoose预定义修饰符 lowercase. uppercase . trim var UserSchema=mongoose.Schema({ name:{ type:String, tr ...
- C#基础回顾(一)—C#访问修饰符
一.写在前面的话 好久没有停下来总结自己,转眼间15年过去好些天,回首过去的日子,亦或失去,亦或所得!生活的节奏,常常让我们带着急急忙忙的节奏去追赶,也许这并不是每个人所期望的生活方式!于他人,于自己 ...
- Vue.js学习笔记(三) - 修饰符
本篇将简单介绍常用的修饰符. 在上一篇中,介绍了 v-model 和 v-on 简单用法.除了常规用法,这些指令也支持特殊方式绑定方法,以修饰符的方式实现.通常都是在指令后面用小数点“.”连接修饰符名 ...
- ch3-模板语法({{}} v-html v-bind:id 表达式 指令 修饰符 过滤器)
1 模板语法 Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. 所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器 ...
随机推荐
- js:输入字数限制
Demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Oracle SQL执行缓慢的原因以及解决方案
以下的文章抓哟是对Oracle SQL执行缓慢的原因的分析,如果Oracle数据库中的某张表的相关数据已是2亿多时,同时此表也创建了相关的4个独立的相关索引.由于业务方面的需要,每天需分两次向此表中 ...
- python2和python3输入输出相关
python3: #coding=utf-8 a = input("请输入你的名字:") print("%s"%a) #输出没有一点问题,a就是字符串(或者数字 ...
- kibana-sentinl-监控报警
kibana 安装 sentin 插件 ./bin/kibana-plugin install https://github.com/sirensolutions/sentinl/releases/d ...
- vc2005(visual studio)使用习惯记录
来源:http://blog.csdn.net/zdl1016/article/details/6184549 前言:sourceinsight不支持显示utf-8的文件, 实在是一大遗憾!vim现在 ...
- ylbtech-LanguageSamples-Porperties(属性)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Porperties(属性) 1.A,示例(Sample) 返回顶部 “属性”示例 本示 ...
- ylbtech-LanguageSamples-SimpleVariance
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-SimpleVariance 1.A,示例(Sample) 返回顶部 SimpleVar ...
- MySql Replication基本原理
Replication的思想是将数据在集群的多个节点同步.备份,以提高集群数据的可用性(HA):Mysql使用Replication架构来实现上述目的,同时可以提升了集群整体的并发能力.5.6版本作为 ...
- flask使用ajax上传图片或者文件
function upload_cover(){ var cover = new FormData(); var fileObj = document.getElementById('cover'). ...
- axios踩坑记录+拦截器使用+vue cli代理跨域proxy+webpack打包部署到服务器
1.小小的提一下vue cli脚手架前端调后端数据接口时候的本地代理跨域问题,如我在本地localhost访问接口http://40.00.100.100:3002/是要跨域的,相当于浏览器设置了一道 ...