1. Insert operation
user = {"username": "LCQ", "Sex": "Man"}db.user.insert (user)
2. Update
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 Delete a field
> 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 to set a field's self-increment
> 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 add a new value to the array
> db.user.update ({"username": "LCQ"},{$addToSet: {"Friends": ' Lisi '}}) >> Db.user.findOne () {"_id": ObjectId ( "55024de2849b70a4f670f413"), "address": {"province": "Shandong", "Location": "Zou Pin "}," Count ": 3," Friends ": [" Zhangsan "," Lisi "], "Haha": "AFDFDFD", "Sex": "Man", "username": "LCQ"}> db.user.update ({"username": "LCQ"},{$addToSet: {"fr Iends ": ' Xiaoming '}}" >>> Db.user.findOne () {"_id": ObjectId ("55024de2849b70a4f670f413"), "address" : {"Province": "Shandong", "Location": "Zoupin"}, "Count": 3, "Fri Ends ": [" Zhangsan "," Lisi "," xiaoming "]," haha ":" AFDFDFD ", "Sex": "Man", "username": "LCQ"}>
Visible when added using $addtoset, if the same value is present, it is not added.
> 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"}
The array is positioned using the $ character to update the value at a specific position in the array.
> 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 = 1010 >> Db.user.save (LCQ) >> Db.user.findOne () { "_id": ObjectId ("55024de2849b70a4f670f413"), " Address ": { " province ":" Shandong ", " location ":" Zoupin " }, " Count ": Ten, " Friends ": [ "Zhangsan", "join", " xiaoming" ], "haha": "AFDFDFD", "sex": "Man", "username": "LCQ "}
The Save method is created when it does not exist, and is updated when it exists. More convenient.
3. Delete
> Db.user.insert ({"username": "Zhangsan"}) > Db.user.remove ({"username": "Zhangsan"})
MongoDB Create, UPDATE, delete