返回介绍

update 数组修改器

发布于 2024-12-09 12:55:35 字数 7620 浏览 0 评论 0 收藏 0

$push 修改器

$push 的功能是追加数组中的值,但我们也经常用它操作内嵌稳文档,就是{}对象型的值。具体代码如下:

// 给 lisi 增加爱好 4,是数组形式的
> db.student.update({name: 'lisi'}, {$push: {"hobby.hobbyFour": "台球"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球", "hobbyFour" : [ "台球" ] }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "" }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "" }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z"), "address" : "" }
{ "_id" : ObjectId("5cbac8243ba35f60e25bffff"), "name" : "zhaoliu", "age" : 11, "score" : [ 89 ] }

$ne 修改器

$ne 修改器的主要作用是:检查一个值是否存在,如果不存在再执行操作,存在就不执行。

// 如果 lisi 的 skill 中没有开车,则添加
> db.student.update({name: 'lisi', skill:{$ne: '开车'}},{$push: {skill: '开车'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球", "hobbyFour" : [ "台球" ] }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "开车" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbac8243ba35f60e25bffff"), "name" : "zhaoliu", "age" : 11, "score" : [ 89 ], "skill" : [ ] }

总结:没有则修改,有则不修改。

$addToSet 修改器

$addToSet$ne 的升级版本,查找是否存在,不存在就 push 进去,操作起来更直观和方便,所以在工作中用的更多。

// 查看 lisi 有没有 react 技能,没有则添加
> db.student.update({name: 'lisi'}, {$addToSet: {skill:'react'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球", "hobbyFour" : [ "台球" ] }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "开车", "react" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbac8243ba35f60e25bffff"), "name" : "zhaoliu", "age" : 11, "score" : [ 89 ], "skill" : [ ] }

$each 修改器

$each 用于:批量添加,可以传入一个数组,一次增加多个值进去,相当于批量操作,性能同样比循环操作要好很多,这个是需要我们注意的,工作中也要先组合成数组,然后用批量的形式进行操作。

var tempSkill = ['react', 'vue', 'webpack']
// 同时给 wangwu 增加 3 个 skill
> db.student.update({name: 'wangwu'}, {$addToSet:{skill:{$each:tempSkill}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球", "hobbyFour" : [ "台球" ] }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "开车", "react" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "react", "vue", "webpack" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbac8243ba35f60e25bffff"), "name" : "zhaoliu", "age" : 11, "score" : [ 89 ], "skill" : [ ] }

$pop 修改器

$pop 只删除一次,并不是删除所有数组中的值。而且它有两个选项,一个是 1 和-1。

  • 1:从数组末端进行删除
  • -1:从数组开端进行删除
// 删除 wangwu 的最后一项 skill
> db.student.update({name: 'wangwu'},{$pop:{skill:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球", "hobbyFour" : [ "台球" ] }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "开车", "react" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "react", "vue" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbac8243ba35f60e25bffff"), "name" : "zhaoliu", "age" : 11, "score" : [ 89 ], "skill" : [ ] }

数组定位修改

有时候只知道修改数组的第几位,但并不知道是什么,这时候我们可以使用 数据项.int 的形式。

// 修改 wangwu 的第 2 个 skill 为 san,需要注意这里的计数是从 0 开始的
> db.student.update({name: 'wangwu'},{$set:{'skill.1':'san'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球", "hobbyFour" : [ "台球" ] }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "开车", "react" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z"), "address" : "", "skill" : [ "react", "san" ] }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z"), "address" : "", "skill" : [ ] }
{ "_id" : ObjectId("5cbac8243ba35f60e25bffff"), "name" : "zhaoliu", "age" : 11, "score" : [ 89 ], "skill" : [ ] }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文