MongoDB修改器的使用2
1."$inc"的使用
主要用来增加数值,比如网站的访问量,点击量,流量等
1 db.games.insert({game:"pinball",user:"joe"}) 2 db.games.find() 3 db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":50}}) 4 db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":10000}})
运行结果分别为
1 /* 1 */ 2 { 3 "_id" : ObjectId("575a37da8fd26400d774e00a"), 4 "game" : "pinball", 5 "user" : "joe", 6 "score" : 10150.0 7 }
db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":50}})
1 /* 1 */ 2 { 3 "_id" : ObjectId("575a37da8fd26400d774e00a"), 4 "game" : "pinball", 5 "user" : "joe", 6 "score" : 10200.0 7 }