1 Database additions and deletions
One, add a database:
Use blog-----switch to the specified database, if the database does not exist, the database is automatically created (new database, if no corresponding collection is stored, is not displayed)
Second, delete a database
1 First switch to the database you need to delete with use Use Test2 then executes the db.dropdatabase () command db.dropdatabase ()
Third, modify the name of a database
db.copydatabase (Fromdb, Todb, fromhost) Fromdb: source database, Todb: Target database fromhost: Original Address instance: Modify the blog's database named Blog123 Use blogdb.copydatabase ("blog", "Blog3");d b.dropdatabase ()
Iv. Querying all databases
Show DBS
2 additions and deletions to documents (collection---tables, documents-rows, fields-columns)
First, add the document
Db.table_name. Insert (document): Insert Db.table_name. Save (document): Insert the documentation, if it has an ID, update note that document is a JSON format instance: Insert a user instance db. user. Insert ({"Name": "Zhangsan", "Password": "123456"}) or DB. user. Save ({"Name": "Zhangsan", "Password": "123456"})
Second, delete the document
Db.table_name.remove (Query,juseone) Note: Both query and Justone are JSON-formatted query: Deleted query Criteria Justone: Delete Only one document example: Delete only one document DB with Name equals test. user. Remove ({"Name": "Test"},{justone:1})
Third, update the document
UPDATE statement:
Db.collection.Update( <Query>, <Update>, {upsert:<Boolean>, Multi:<Boolean>, Writeconcern:<Document>}) Parameter description: The query:update query condition, similar to where in SQL update query. Update: Update objects and some updated operators (such as $, $inc ... ), and so on, can also be understood as Upsert in SQL Update query after set: optional, this parameter means that if there is no record of the update, insert Objnew,true is inserted, default is False, not inserted. Multi: Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all. Writeconcern: Optional, throws an exception level. Example: Update the password for name Zhangsan to 111111, insert if it does not exist, or update only one if there are multiple bars Useblogdb.User.Update({"Name": "Zhangsan"},{$set: {"Password": "111111"},true,false}")
Updated operator description for UPDATE statement:
1$set: Equivalent to the equal sign "=", or a new field for the document, such as name does not exist, add a name field for example: {$set: {"name": "" 123 "}}2$inc: Equal to" plus "" + = ", the field must exist, for example: {$inc: { "Ago": 1}}3$unset: Remove a field from the document for example: {$unset: {"Ago": 0}}4$push: Adds an array value for a field in the document, and if the field does not exist, creates a field original record: {"_id": ObjectId (" 5003be465af21ff428dafbe7 ")," name ":" Zhangsan "," Password ":" 123456 "," group ": [" Manager "]} Execute command db.user.update ({" Name ":" Zhangsan "},{$push: {" group ":" Sale "}) after execution {" _id ": ObjectId (" 5003be465af21ff428dafbe7 ")," name ":" Zhangsan "," Password ":" 123456 "," group ": [" manager "," sale "]}5$ne when adding an element primarily to the array type key value, avoid generating duplicate data in the array, $ne in some cases is not passable. Db.user.update ({"group": {$ne: "Sale"}},{$push: {"group": "Sale"}}) 6. Array modifier--$pop, $pull $ Pop removes elements from the array at the head or tail of the array-removing 1> db.c.update ({"Name": "Zhangsan"},{$pop: {"group": 1}}) from the end of the array $pull remove the element that satisfies the condition from the array, as shown in the following example: > db.user.update ({"Name": "Zhangsan"},{$pull: {"group": "Sale"}}) 7. The positioning modifier of an array-------------------------------------------------------------------can be manipulated by the position or position operator ("$") when the value in the set is required ). The array starts at 0, and you can select the element directly as a key. The example is as follows: {"UID": "001", comments:[{"name": "T1", "Size": 10},{"name": "T2", "Size":12}]}> db.c.find ({"UID": "001"}) {"_id": ObjectId ("5003da405af21ff428dafbe8"), "UID": "001" , "comments": [{"Name": "T1", "Size": ten}, {"name": "T2", "size": []]}> db.c.update ({"UID": "001"},{$inc: {"C Omments.0.size ": 1}}" > Db.c.find ({"UID": "001"}) {"_id": ObjectId ("5003da405af21ff428dafbe8"), "UID": "001", " Comments ": [{" Name ":" T1 "," Size ": one}, {" name ":" T2 "," Size ": 12}]}
Save () statement:
The Save () method replaces an existing document with an incoming document. The syntax format is as follows: Db.collection.save ( <document>, { writeconcern: <document> }) Document:json Format Example: Update the password for name Zhangsan to 123456use blogdb.user.save ({"_id": ObjectId ("574d9667ec9f7148b53f27d9"), " Name ":" Zhangsan "," Password ":" 123456 "})
Iv. Querying documents
Db.table_name.find (query) Query:json format, you can use the operator example: query a record that is greater than 20 before and (name equals Zhang San or password equals 123456) db.col.find ({"Ago": {$ GT:20}, $or: [{"Name": "Zhangsan"},{"password": "123456"}]}). Pretty () Db.col.find (). Pretty (): Display the contents of the query in a reasonable format
MongoDB Learning Note One (collection and document additions and deletions)