About MongoDB related knowledge, reading notes, easy to consult with, not regularly updated (pure hand) "MongoDB authoritative guide"
First, create updates and deletes
1. Create
// bulk inserting a collection can save time, using only one TCP // Bson when inserted, and data cannot be greater than 4MB Db.blog.insert ({// Insert a document document has a name key
var temp={"name": "Dog"} Db.blog.insert (temp) // Insert a document document has a name key
2. Delete
Db.blog.remove ({}) //db.blog.remove ({"Age": "}") // Delete all documents with age 18 in the blog // if the pursuit speed deletes the entire collection and then re-creates the index it will be quicker to delete all documents Db.blog.drop () // not verified if it's really much faster
3. Update
Db.blogs.insert ({"Name": "Joe", Age:18,frineds: "Tom"})//The following is the update step var joe= Db.blogs.findOne ({"Name": "Joe"}) // not available here find () find returns cursor joe.relationship={"Friends" : "Kate"}joe.age=19db.blogs.update ({"name": "Joe"},joe); // Update the modified document if more than one document named Joe exists in the collection, an attempt is made to update more than one, but the _id in Joe is fixed, causing an error. Be aware of uniqueness when updating
3.1 Modifiers
In general, you only need to modify some of the contents of the document, it is appropriate to use the modifier (atomicity)
// EG1 A document that records the number of times a page is accessed, adding a $inc to the subsequent key plus the specified value (negative for subtraction) if the key does not exist and adds the build to the specified value db.web.insert ({" PageName ":" Cnblogs "," Times ": 1}) db.web.update ({" pageName ":" Cnblogs "},{" $inc ": {" Times ": 1}})
// EG2 directly modifies the value $set directly modifies if the modified key does not exist, the key is added and set to the specified value db.web.update ({"PageName": "Cnblogs"},{"$set": {"Times ":")//eg3 Modify the inline value db.blog.update ({"Name": "Joe"},{"$set": {"Relationship.friends": "Lin"}})
// EG4 Deletes the specified key $unset deletes the specified key in this example, the age key is removed
// the operation of an array $push inserted at the end of the Db.web.insert ({"Name": "Test", "Bin": [1,2,3,4,6]}) db.web.update ({" Name ":" Test "},{" $push ": {Bin:5}}) Db.web.insert ({" name ":" Try "," bin ": [{" id ": 1},{" id ": 2}]}) Db.web.update ({"name": "Try"},{"$push": {"bin": {"id": 3}})
// array Operation $ne mate $pushDb.web.insert ({"Name": 1, "bin": []});d B.web.insert ({"name": 2, "bin": [ 1,2,3,4]});d b.web.update ({"name": {"$ne": 1}},{"$push": {"Bin": 4}})// Add 4 for bin in the document with Name 1// If 4 is not in Bin, add 4
// Addtoset adds a combination of $ne and $push instead of db.web.update ({// For a document with name 1, if no 20 in bin is added
// add different contents to the array $addToSet mates $each db.web.update ({"id": 1},{"$addToSet": {"bin": {"$each": [1,2,3,4,5]}}})
// Delete element in array $popdb.web.update ({"id": 1},{"$pop": {"bin": 1}}) // Remove a db.web.update ({"id": 1},{"$pop": {"bin": -1}}) / / Delete a / /From the beginning of the array Delete element in array $pulldb.web.update ({"id": 1},{"$pull": {"bin": 1}}) // Remove all items that are 1 from the array
// Array positioning operation $db.web.update ({"bin": 3},{"$inc": {"bin.$": 3}})
// $upsert There is an update, no then adds a new document based on query criteria and updates the third parameter of update is true for Upsertdb.web.update ({"id": 3},{"$push" : {"bin": 8}},true)
// $save exist _id make call Upsert otherwise call Insertvar x=Db.web.findone (); X.id=9 Db.web.save (x)
// updating multiple documents that match // under normal circumstances, even if multiple matches are only updated to match the first one, the fourth parameter of update is true to match more than one db.web.update ("id": {"$ne": 100},{"&push": {"bin": +},false,true// Add the bin for all documents with ID not 100
Second, query
// separated by commas for multiple conditional conditions 1and condition 2db.web.find ({"id": 1, "name": "Blog"})
// returns the second parameter of the Find function that specifies the key to return and does not need to return a key of 1 to return 0 for the need to return Db.web.find ({"id": 1},{"id": 1, "_id:0"})
// $lt $lte $gt $gte respectively < <= > >=db.web.find ({//
var date=New Datedb.web.find ({"datetime": {"$GT":d ate}})
// $in One of the key values matches any one of the criteria $or reach db.web.find ({"id": {"$in": [Three-year]}) find document with ID 1 2 or 3 db.web.find ({"$or": [{"id": 1},{"Age": 3}]}) // find documents with ID 1 or age 3
// $mod Withdraw $not take the inverse db.web.find ({"id": {"not": {"$mod: [5,1]"}}}) // take ID No more than 1 of IDs, such as 2,3,4,5,7, after 5
// Regular Expression /xxxxxxxxxx/db.web.find ({"name":/[0-9]*/}) // find documents with names as numbers
// Query Array $all db.web.find ({"bin": {"$all": [[i]}}) // Find documents in bin that contain 1, 2, and 3
MongoDB Reading Notes (Junior command)