标签:mongodb
1.插入操作user = {"username":"lcq","sex":"man"} db.user.insert(user)
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}})
> 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}})
> 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" }
> 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" } >
> 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" }
> db.user.insert({"username":"zhangsan"}) > db.user.remove({"username":"zhangsan"})
标签:mongodb
原文地址:http://blog.csdn.net/liuchangqing123/article/details/44240417