大家好,又见面了,我是全栈君。
user = {"username":"lcq","sex":"man"} db.user.insert(user)
2.更新
var lcq = db.user.findOne({"username":"lcq"}} lcq.address = {"province":"shandong","location":"zoupin"} lcq.haha = "hehhehe" db.user.update({"username":"lcq"},lcq) > db.user.update({"username":"lcq"},{$set:{"test":"test"}}) > db.user.findOne({"username":"lcq"}) { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "haha" : "afdfdfd", "sex" : "man", "test" : "test", "username" : "lcq" }
> db.user.update({"username":"lcq"},{$unset:{"test":1}})
$unset删除某个字段
> lcq = db.user.findOne({"username":"lcq"}) { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" } > > > lcq.count = 1 > db.user.update({"username":"lcq"},lcq) > db.user.update({"username":"lcq"},{$inc:{"count":2}})
$inc设置某个字段自增
> db.user.update({"username":"lcq"},{$set:{"friends":['zhangsan']}}) > db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 3, "friends" : [ "zhangsan" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" } > db.user.update({"username":"lcq"},{$push:{"friends":'lisi'}}) > > db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 3, "friends" : [ "zhangsan", "lisi" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" }
$push向数组中加入新的值
> db.user.update({"username":"lcq"},{$addToSet:{"friends":'lisi'}}) > > db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 3, "friends" : [ "zhangsan", "lisi" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" } > db.user.update({"username":"lcq"},{$addToSet:{"friends":'xiaoming'}}) > > > db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 3, "friends" : [ "zhangsan", "lisi", "xiaoming" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" } >
可见使用$addToSet加入时,假设存在同样的值则不加入。
> db.user.update({"friends":"lisi"},{$set:{"friends.$":"join"}}) > > > > db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 3, "friends" : [ "zhangsan", "join", "xiaoming" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" }
使用$符进行数组的定位,实现更新数组特定位置的值。
> lcq = db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 3, "friends" : [ "zhangsan", "join", "xiaoming" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" } > > > lcq.count = 10 10 > > db.user.save(lcq) > > db.user.findOne() { "_id" : ObjectId("55024de2849b70a4f670f413"), "address" : { "province" : "shandong", "location" : "zoupin" }, "count" : 10, "friends" : [ "zhangsan", "join", "xiaoming" ], "haha" : "afdfdfd", "sex" : "man", "username" : "lcq" }
save方法在不存在时创建。存在时更新。
更加方便。
3.删除
> db.user.insert({"username":"zhangsan"}) > db.user.remove({"username":"zhangsan"})
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115904.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...