文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
update 修改器
$set 修改器
$set
修改器用来修改一个指定的键值(key)。
// 修改
> db.student.update({"name": "lisi"}, {"$set":{sex: 0, age: 20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
// 查看修改后的数据
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyOne" : "篮球", "hobbyTwo" : "足球", "hobbyThree" : "羽毛球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 13, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z") }
修改嵌套内容
比如现在的 lisi
的爱好发生了变化,说她不会打篮球而是跳绳很好,需要进行修改。这时候我们会发现 hobby 数据是内嵌的,这时候我们可以属性的形式进行修改 hobby.hobbyOne
,代码如下:
// 修改
> db.student.update({"name": "lisi"}, {"$set": {"hobby.hobbyOne": "跳绳"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
// 查看
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyOne" : "跳绳", "hobbyTwo" : "足球", "hobbyThree" : "羽毛球"}, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 13, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z") }
$unset 修改器
$unset
的作用是:删除一个 key 值和键。当我们删除后,想加回来可以直接用 $set
进行添加。
// 删除 hobby.hobbyOne
> db.student.update({"name":"lisi"},{$unset:{"hobby.hobbyOne":""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
// 查询
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 13, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z") }
// 重新添加
> db.student.update({"name":"lisi"},{$set:{"hobby.hobbyOne":"篮球"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 13, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z") }
$inc 修改器
$inc
修改器用于对数字进行计算,可以对 value 值进行修改,但是修改的必须是数字,字符串是不起作用的。现在把 xiaohong
的年龄减去 3 岁,就可以直接用 $inc
来操作。
> db.student.update({"name":"xiaohong"},{$inc: {"age": -3}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6e"), "name" : "wangwu", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "游泳", "hobbyTwo" : "健身", "hobbyThree" : "乒乓球" }, "createTime" : ISODate("2019-04-20T06:33:15.407Z") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z") }
multi 选项
把每个学生的住址也加进来
// 这种写法只会改变第一条数据
> db.student.update({}, {$set:{address: ''}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球" }, "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") }
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6f"), "name" : "xiaohong", "age" : 10, "sex" : 0, "hobby" : { "hobbyOne" : "瑜伽", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "createTime" : ISODate("2019-04-20T06:33:46.360Z") }
// 加上{multi:true},则是改变所有的数据项
// multi 可取 ture 和 false 两个值,true 代表全部修改,false 代表只修改一个
> db.student.update({}, {$set:{address: ''}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球" }, "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" : "" }
upsert 选项
upsert 选项作用是:在找不到的情况下,直接插入这条数据。
> db.student.update({name: 'zhaoliu'}, {$set: {age: 11}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球" }, "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" : "" }
// upsert 也有两个值:true 代表没有就添加,false 代表没有不添加(默认值)
> db.student.update({name: 'zhaoliu'}, {$set: {age: 11}}, {upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5cbac8243ba35f60e25bffff")
})
> db.student.find()
{ "_id" : ObjectId("5cbabd2b0712a21a9fdb3b6d"), "name" : "lisi", "age" : 20, "sex" : 0, "hobby" : { "hobbyTwo" : "足球", "hobbyThree" : "羽毛球", "hobbyOne" : "篮球" }, "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 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论