In the previous chapter, there is a way to insert a document by inserting it . If the collection is not in the database,MongoDB automatically creates the set and inserts the document
Find all the collection data using the Find method
> Db.maple.find ()
{"_id": ObjectId ("5a35d6278ef76f6d57aae92c"), "name": "ZHF"
You can also define the data as a variable:
Document= ({"Name": "ZHF"})
Db.maple.insert (document)
Document Update:
MongoDB uses the update () and Save () methods to update the documents in the collection
Parameter description:
The query:update query condition, similar to that in SQL update query , is behind.
Update:update objects and some updated operators (such as $, $inc ... ) and so on, can also be understood as SQL Update query Inside Set behind the
Upsert: Optional, this parameter means that if there is no Record of update, insert objnew,true as INSERT, default is false , not inserted.
Multi: optional,mongodb defaults to false, updating only the first record found, if this parameter is true, you can find out all the records on a per-condition basis.
Writeconcern: Optional, throws an exception level.
Db.maple.update ({"Name": "ZHF"},{"$set": {"name": "Zhanghongfeng"}})
The operator can also be used to judge the way
> db.maple.update ({"Age": {"$eq": 35}},{' $set ": {" City ":" Chengdu "}})
# Find documents with age equal to and set the city to Chengdu
> db.maple.update ({"City": "Chengdu"},{"$inc": {' Age ': 1}})
# Find documents for Chengdu in the city and Add age 1
> db.maple.update ({"City": "Chengdu"},{"$inc": {' age ':-1}})
# Find documents for Chengdu in the city and subtract age by 1
#unset: Delete a field
> db.maple.update ({"Age": 35},{$unset: {"Programming": 1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.maple.find ()
{"_id": ObjectId ("5a35d6278ef76f6d57aae92c"), "name": "Zhanghongfeng_maple"}
{"_id": ObjectId ("5a35d7ce8ef76f6d57aae92d"), "age": +, "City": "Chongqing"}
{"_id": ObjectId ("5a35d8548ef76f6d57aae92e"), "favorite": "Basketball"}
{"_id": ObjectId ("5a3611de366b42e0050debd8"), "coding": ["C", "Python", "Go", "Java"]}
$push operation: Append Value To field, field must be array type, if field does not exist, a new array type will be added.
> db.maple.update ({"Age": 35},{$push: {"Programming": "JavaScript"}})
> Db.maple.find ()
{"_id": ObjectId ("5a35d6278ef76f6d57aae92c"), "name": "Zhanghongfeng_maple"}
{"_id": ObjectId ("5a35d7ce8ef76f6d57aae92d"), "age": +, "City": "Chongqing", "Programming": ["JavaScript"]}
{"_id": ObjectId ("5a35d8548ef76f6d57aae92e"), "favorite": "Basketball"}
{"_id": ObjectId ("5a3611de366b42e0050debd8"), "coding": ["C", "Python", "Go", "Java"]}
# An array is added when the array does not exist
> db.maple.update ({"Age": 35},{$push: {"Programming": "MySQL"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.maple.find ()
{"_id": ObjectId ("5a35d6278ef76f6d57aae92c"), "name": "Zhanghongfeng_maple"}
{"_id": ObjectId ("5a35d7ce8ef76f6d57aae92d"), "age": +, "City": "Chongqing", "Programming": ["JavaScript", "MySQL" ] }
{"_id": ObjectId ("5a35d8548ef76f6d57aae92e"), "favorite": "Basketball"}
{"_id": ObjectId ("5a3611de366b42e0050debd8"), "coding": ["C", "Python", "Go", "Java"]}
# Existing cases will continue to add data to the array
> db.maple.update ({"Age": 35},{$pushAll: {"Programming": ["Object-c", "HTML"]}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.maple.find ()
{"_id": ObjectId ("5a35d6278ef76f6d57aae92c"), "name": "Zhanghongfeng_maple"}
{"_id": ObjectId ("5a35d7ce8ef76f6d57aae92d"), "age": +, "City": "Chongqing", "Programming": ["JavaScript", "MySQL", ["MongoDB", "Linux"], "object-c", "HTML"]}
{"_id": ObjectId ("5a35d8548ef76f6d57aae92e"), "favorite": "Basketball"}
{"_id": ObjectId ("5a3611de366b42e0050debd8"), "coding": ["C", "Python", "Go", "Java"]}
# add multiple data to the array using the Pushall method
#addToSet
Add only when the array does not exist:
> db.maple.update ({"Age": 35},{$addToSet: {"Programming": "MySQL"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 0})
#pop
Delete First and last element
> db.maple.update ({"Age": 35},{$pop: {"Programming": 1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> db.maple.update ({"Age": 35},{$pop: {"Programming":-1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
#pull: Delete a specific element
> db.maple.update ({"Age": 35},{$pull: {"Programming": "Object-c"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
Document Lookup:
Let's look at the comparison in the relational database:
Db.maple.find ()equivalentselect * from Maple
Db.maple.find ({"Age": 27})equivalentselect * from Maplewhere age= ' 27 '
Db.maple.find ({"Age": +, "name": "Xing"})equivalentselect * from MapleWhere age= ' and name= ' Xing '
Db.maple.find ({},{"name": 1}) select name from Maple,ifname:0It is not displayednameField
Db.maple.find (). Limit (1)equivalentselect * from MapleLimit 1
Db.maple.find (). Sort ({_id:-1}). Limit (1) equals select * from maple order by _id DESC LIMIT 1
Db.maple.find ({"Age": {"$in", [12,34,100]}})equivalentSELECT * fromMaple where age in (12,3,100)
Db.maple.find ({"Age": {"$nin", [12,3,100]}})equivalentSELECT * fromMaple where _id not in (12,3,100)
Db.maple.find ({"$or": [{"Age": 16},{"name": "Xing"}]})equivalentselect * FROM blog where age = + or name = ' xing '
Db.maple.find ({"Id_num": {"$mod": [5,1]}})Take theid_num MoD 5 = 1The fields, such asid_num=1,6,11,16
Db.maple.find ({"Id_num": {"$not": {"$mod": [5,1]}}})Take theid_num MoD 5! = 1fields, such as in addition to theid_num=1,6,11,16all fields, more than regular, are used together.
$exists determine if a field exists
Db.blog.find ({"name": {$exists: true}}); if the element name exists , it returns
Db.blog.find ({"name": {$exists: false}}); if the element name does not exist , it returns
# Query array:
Db.blog.find ({"coding": {"$size": 4}}) coding array with a length of 3 matches the result
Db.blog.find ({"coding": {"$all": ["C", "Python", "Go", "Java"]}}) must have each of the arrays in the fruit to match the result
MongoDB Learning: Document manipulation