1.插入操作
2.更新
$unset删除某个字段
$inc设置某个字段自增
$push向数组中加入新的值
可见使用$addToSet加入时,假设存在同样的值则不加入。
使用$符进行数组的定位,实现更新数组特定位置的值。
save方法在不存在时创建。存在时更新。
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"})