MongoDB 修改器
2016-01-06 00:00
766 查看
摘要: MongoDB 修改器
$inc 修改器是 增加或者 减少相应的 数值。
$set 修改器是 指定一个键的值
$inc 修改增加的值 //demo: //查看已有的一条数据 db.stone.find(); //查询结果: { "_id" : ObjectId("568b657d30b22da545433c22"), "name" : "stone", "age" : 118 } //$inc 值能修改数值类型 db.stone.update({"name":"stone"},{"$inc":{"age":1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) //成功提示 //如果$inc 修改不是 整形或者浮点型 就会报错 //插入一条数据,age 设置为字符串类型(关于mongodb的数据类型,目前还没有接触, //以后会专门拿出出时间学习一下数据类型) db.stone.insert({"name":"stoneString","age":"18"}) //修改 age db.stone.update({"name":"stoneString"},{"$inc":{"age":1}}) //报错如下 WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "Cannot apply $inc to a value of non-numeric type. {_ id: ObjectId('568cb1793c8dd16060b20a42')} has the field 'age' of non-numeric typ e String" } }) //Cannot apply $inc to a value of non-numeric type.
$inc 修改器是 增加或者 减少相应的 数值。
$set 修改器是 指定一个键的值
//查询已有数据 db.stone.find(); { "_id" : ObjectId("568b657d30b22da545433c22"), "name" : "stone", "age" : 119 } { "_id" : ObjectId("568cb1793c8dd16060b20a42"), "name" : "stoneString", "age" :"18" } //设置 stoneString age 为 77 db.stone.update({"name":"stoneString"},{"$set":{"age":77}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) //成功提示 //查看结果 db.stone.find(); { "_id" : ObjectId("568b657d30b22da545433c22"), "name" : "stone", "age" : 119 } { "_id" : ObjectId("568cb1793c8dd16060b20a42"), "name" : "stoneString", "age" :77 } //$set 可以修改数据类型 字符串 修改为 数值 db.stone.update({"name":"stoneString"},{"$set":{"age":77}}) { "_id" : ObjectId("568b657d30b22da545433c22"), "name" : "stone", "age" : 119 } { "_id" : ObjectId("568cb1793c8dd16060b20a42"), "name" : "stoneString", "age" :77 }
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 如何在 Fedora 上安装 MongoDB 服务器
- PHP添加yaf xhprof mongodb 同理
- mongodb安装
- 如何在 Ubuntu 上安装 MongoDB
- perl操作MongoDB报错undefined symbol: HeUTF8解决方法
- C#中使用1.7版本驱动操作MongoDB简单例子
- MongoDB系列教程(四):设置用户访问权限
- php实现的mongodb操作类实例
- 解决mongodb在ubuntu下启动失败,提示couldn‘t remove fs lock errno:9 Bad file descriptor的错误
- 在PostgreSQL的基础上创建一个MongoDB的副本的教程
- 关于mongoose连接mongodb重复访问报错的解决办法
- java操作mongodb示例分享
- php对mongodb的扩展(初出茅庐)
- 作为PHP程序员应该了解MongoDB的五件事
- 基于MySQL到MongoDB简易对照表的详解
- MongoDB入门教程之C#驱动操作实例
- MongoDB为用户设置访问权限
- MongoDB db.serverStatus()输出内容中文注释
- MongoDB的一些常用查询方法