文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
update 数组修改器
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论