db
View the database for the current operation
show dbs
Show all databases
show collections
Displays all collections under the current database
use database_name
Connect to a database named "database_name" and create if it does not exist
db.dropDatabase()
Delete current Database
db.collection_name.drop()
Delete named "Collection_name" collection
db.createCollection(‘collection_name‘ , {capped : true ,size : 100000} )
Create a collection called "collection_name", specifying a length of 100000b
db.collection_name.find({})
The query is called "collection_name" in the collection of all documents, curly braces for the query criteria, unconditional query all.
db.collection_name.find({}).pretty()
Query all the documents in the collection, pretty to beautify the display effect.
db.collection_name.find({}).limit(1).skip(2)
Pagination Displays the result of the query, limit is the starting position, and skip is the number of bars taken.
db.collection_name.find({}).sort({KEY:1})
Sort for sorting criteria, 1 for ascending, 1 for descending
db.collection_name.insert({‘name‘:‘yan‘})
Inserts a document into the collection "collection_name".
' Db.collection_name.save ({}) '
Save the data, and if you do not specify an ID, the effect is the same as insert, which is the updated action if the ID is specified.
db.collection_name.update({<query>,<update>,{upsert:<boolean>,multi:<boolean>,writeConcern:<document>}})
Update document
- Query: Search criteria
- Update: Fields that need to be updated
- Upsert: Optional (false by default), set to true: Inserts are made if there are no documents that meet the criteria of the query.
- Multi: Optional (false by default), set to true: if there are more than one document that meets the query criteria, all of the documents are updated. Set false: Updates only the first document to the query.
- Writeconcern: Optional, throws an exception level
db.collection_name.remove(<query>,{justOne:<boolean>,writeConcern:<document>})
Delete
db.collection_name.ensureIndex({KEY:1})
Index, can write multiple (composite index)
db.collection.aggregate( [ $group : { _id : ‘$__xh‘ , num_tutorial : {$sum : 1} } ] )
Aggregation, which is an advanced query, for example, the total number of statistical classes
MongoDB Common directives