---------------------MongoDB basic Operation---------------------1. MongoDB CREATE DATABASEUse database name: Switch to the specified database and create the corresponding database when inserting the first bar data. Show DBS: Displays all the database names. db: View the name of the current database. Show Collections: View all collections of the current DB. db. Collection name. Insert ({"name": "Python"}): Adds a piece of data to the specified collection. 2. MongoDB Delete Databasedb.dropdatabase (): Deletes the current database. 3. MongoDB Create collectiondb.createcollection (name,options):parameter Type descriptionName String to create the collection nameOptions Document (optional) Specify the memory size and index options field type descriptioncapped Boolean (optional) If true, the CAP collection is enabled. A cap set is a fixed-size collection that automatically overwrites the oldest entries when it reaches its maximum size. If True is specified, the Size field needs to be specified as well. Autoindexid Boolean (optional) If true, the index _id field is automatically created, and the default value is False. size number (optional) specifies the maximum number of bytes that can be used for the collection. If the cap is true, then you also need to specify this field. max Number (optional) specifies the maximum amount that the capping collection allows in the file. The size limit takes precedence over this limit. If a capping collection reaches the size of the limit and the maximum number of files is not reached, MongoDB deletes the old file. If you prefer to use Max, make sure that the size limit that is required for the upper bound collection is sufficient to contain the maximum number of documents. Example: db.createcollection ("Log", {capped:true, size:5242880, max:5000}) 4. MongoDB Delete Collectiondb. Collection name. Drop () 5. MongoDB Data Type1. String: This is the most commonly used data type to store data. The string in MongoDB must be a valid UTF-8. 2, Integer: This type is used to store a numeric value. The integer can be either 32-bit or 64-bit, depending on your server. 3. Boolean: This type is used to store a Boolean value (True/false). 4, Double: This type is used to store floating-point values. 5. Min/max keys: This type is used to compare the lowest and highest values of the Bson element. 6. Arrays: Use this type of array or list or multiple values to store to a key. 7, Timestamp: Time stamp. This makes it easy to record when the file has been modified or added. 8. Object: This data type is used for embedded files. 9, null: This type is used to store a Null value. 10. Symbol: This data type is used for the same string, but it is usually reserved for use by a language of a particular symbol type. 11. Date: This data type is used to store the UNIX time format for the current date or time. You can specify your own date and time, date and year, month, day to create an object. 12. Object ID: This data type is used to store the document ID. 13. Binary data: This data type is used to store the binaries. 14. Code: This data type is used to store JavaScript code in the document. 15. Regular expression: This data type is used to store regular expressions. 6. MongoDB Insert Documentinserting data can use Insert () and Save ():db. Set name. Insert (row data information). Example:Db.mongoDBtest.insert ( {title: ' MongoDB Overview ',Description: ' MongoDB is no SQL database ',by: ' Tutorials itcast ',URL: ' http://www.itcast.cn ',Tags: [' mongodb ', ' database ', ' NoSQL '],likes:100 } ) 7. MongoDB Query Document1. Enquiry:db. Collection name. Find (): Queries the data information for the current collection. db. Collection name. FindOne (): Queries the first data information for the current collection. Pretty (): The results are displayed in a formatted manner. 2. Use of and:in the Find () method, if the ', ' is separated by multiple keys, then MongoDB handles the and conditionSyntax: Db.mongoDBtest.find ({key1:value1, key2:value2}). Pretty () 3. Use of ORto query a file based on the or condition, you need to use the $or keyword. >db.mongodbtest.find ( {$or: [{key1:value1}, {key2:value2} ] }). Pretty () 4, and and or use togetherDb.mongoDBtest.find ({key1:value1,$or: [{key1:value1},{key2:value2} ]}). Pretty () 5. MongoDB Limit and Skip methodlimit: Displays the specified number of data. Skip : Skips the specified number of data. db.mongoDBtest.find (). Limit (Ten). Skip (100): Show 100-110 data 6. MongoDB projectionThe projection means that only the necessary fields are selected in the returned result of the query, not the entire field of a document. If a document has 5 fields, you need to display only 3, projecting 3 of the fields. Syntax:Db.mongoDBtest.find ({},{"name": 1,_id:0}) 7. MongoDB SortGrammarthe basic syntax for the sort () method is as follows:>db. Collection name. Find (). Sort ({key:1})The sort () method allows you to specify the sorted field by parameter, and uses 1 and-one to specify how the sort is sorted, where 1 is in ascending order, and 1 is used in descending order. 8. MongoDB asks for Countuse the Count () method to count dataGrammarthe basic syntax for the count () method is as follows:>db. Collection name. Find ({}). Count ()ordb. Collection name. Count ({})> Db.mongoDBtest.find ({' likes ': {' $gt ':}}). Count () 9, MongoDB Beg distinctuse the Distinct () method to perform deduplication statistics on dataGrammarthe basic syntax of the distinct () method is as follows:>db. Collection name. Distinct (' key ', {})> Db.mongoDBtest.distinct (' title ', {' likes ': {' $gt ':}}) 8. MongoDB Update Documentation1. MongoDB uses the update () and Save () methods to update the documents in the collection. 2. Syntax:the basic syntax for the update () method is as follows Db.mongoDBtest.update (<query>,<update>, {Upsert: <boolean>Multi: <boolean>Writeconcern: <document> } )3, the corresponding parameter in the syntax is introduced:The query:update query condition, similar to that in SQL Update query, is behind. update:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after theUpsert: Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert. 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. 4. Example:>db.mongodbtest.update ({' By ': ' Tutorials-itcast '},{$set: {' by ': ' Itcast '}},{multi:true}) 9. MongoDB Delete Document1. The MongoDB remove () function is used to remove a document from the collection. 2. GrammarThe basic syntax of the Remove () method is as follows Db.collection.remove (<query>, {justone: <boolean>Writeconcern: <document> } )3. Parameter description:query: (optional) The condition of the deleted document. Justone: (optional) If set to TRUE or 1, only one document is deleted. False by defaultWriteconcern: (optional) the level at which the exception is thrown. 4. Example:Db.myLimit.remove ({"_id": {$gt: 0}},1)
MongoDB Basic Shell operation