MongoDb Array Object

[Array]

role:[type:String]
  • $addToSet(each循环) 添加
db.collection.update(
   { <query selector> },
   { $addToSet: { <field1>: <value1>, ... } }
)
db.member.update({_id:ObjectId('594200a1167b4102894b11c0')},{$addToSet:{"role":'VIP'}})
db.collection.update(
   { <query selector> },
   { $addToSet: { <field>: {$each:[ <value1>, <value2>, ... ]} } }
)
  • $push(each循环) 追加
db.collection.update(
   { <query selector> },
   { $push: { <field1>: <value1>, ... }}
)
db.collection.update(
   { <query selector> },
   { $push: { <field>: {$each:[ <value1>, <value2>, ... ]} } }
)
  • $pushAll 批量追加
db.collection.update(
   { <query selector> },
   { $pushAll: { <field>: [ <value1>, <value2>, ... ] } }
)
db.collection.update(
   { <query selector> },
   {
  $push: {
    <field>: {
       $each: [ <value1>, <value2>, ... ],
       $position: <num>
    }
  }
})
  • $pull 删除
db.collection.update(
   { <query selector> },
   { $pull: { <field1>: <value1>, ... } }
)
db.member.update({_id:ObjectId('594200a1167b4102894b11c0')},{$pull:{"role":'VIP'}})
  • pullAll 批量删除
db.collection.update(
   { <query selector> },
   { $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }
)
  • $pop 删除
db.collection.update(
   { <query selector> },
   { $pop: { <field>: <-1 | 1>, ... } }
)
1:左边第一位/-1:右边第一位
  • $set 修改
db.collection.update(
   { <array>: value ... },
   { <update operator>: { "<array>.$" : value } }
)
db.member.update({_id:ObjectId('594200a1167b4102894b11c0'),'role':'VIP'},{$set:{"role.$":'System'}})
  • $slice 合并并保留一定的位数
db.collection.update(
   { <array>: value ... },
   {
  $push: {
     <field>: {
       $each: [ <value1>, <value2>, ... ],
       $slice: <num>
     }
  }
})

num:正数表示从头部(左边)开始,负数表示从尾部(右边)开始

[{Object}]

  • $set 添加/修改
//修改
db.collection.update(
   { <query selector> },
   { $set: { "array.$.field" : value } }
)
//添加
db.member.update({_id:ObjectId('594200a1167b4102894b11c0')},{$set:{"oAuth.weibo":'QQId'}},{upsert:true})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于operators 官方参考文档:https://docs.mongodb.org/manual/refere...
    我看不见阅读 7,344评论 0 3
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,867评论 6 13
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,350评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,408评论 19 139
  • 不管你有再多的兴趣爱好 再多的社会关系 再深的对努力学习的厌恶之感 在你青春期的某一个时间点 你都会明白 学习的重...
    篮球男孩阅读 1,319评论 0 1