Library operations
Show DBS View the list of databases in the system
Note: Content must be present in the database to be displayed
db = Db.getsiblingdb ("< database name >")
Or
Use < database name >
Toggle the current database
Note: If the database does not exist, create the database
Either method is possible, set the DB value to the specified database, and then use DB to manage the new current database.
Db.dropdatabase () Delete the current database
Note: Deleting the current database and then using the handle to create a collection without changing the current database, the deleted database is recreated.
Db.copydatabase ("< current database name >", "< Backup library name >", [hostname])
Copy the database to create a database that is identical to the name
Note: The optional hostname parameter specifies the current library MONGDB server host name.
DB Displays the current database
Collection operations
Show Collections View a list of the collections contained in the current database
Db.createcollection ("< collection name >", {<options>}) Create collection
Note: Parameter two optional objects
Property Description
Capped Boolean, if true, indicates that the collection is a capped collection, and it does not grow larger than the size specified at the largest scale property. Default: False
Autoindexid Boolean, if True, indicates that an _ID field is automatically created for each document added to the collection and that the index on that field is implemented. This pair cap set should be false. Default True
A size byte unit that is used to cap the collection. The oldest file is deleted to make room for the new file
Max Maximum number of documents allowed in the capping collection. The oldest file is deleted to make room for the new file
db.< collection name >.drop ()
Or
coll = db.getcollection ("< set name >")
Coll.drop () Delete collection
db.< collection name >.find ()
Or
coll = db.getcollection ("< set name >")
Coll.find (query) to view a document in a collection
Note: The optional query parameter specifies that the queried document that contains the fields and values matches the document in the collection, and returns the matching content. Example: Coll.find ({speed: "120mph"})
db.< Collection name >.insert ({})
Or
db.< Collection name >.save ({})
inserts the document data in the collection (if the collection does not exist, it is created, save is also)
Treat homogeneous data as a collection
Properties that are specific to the data in the collection
A collection is a container in which n multiple data can be placed inside
This data can be filtered, sorted
db.< Collection Name >.remove ({filter condition},false)
remove a document from the collection parameter 1: Filter criteria. Parameter 2: Optional parameter, whether single row delete, default false.
db.< Collection name >.save ({"_id": ObjectId ("57e26b294a655f35e13d6f5d"), "name": "Hung", "Age": 18})
Or
db.< Collection Name >.update ({filter Condition}, {$set: {"name": "Jin", "Up": true}},
{upsert:true,multi:true}) update a document in a collection
Parameter one: Update the condition of the document
Parameter two: Update operators used when updating, and update properties
Common operators: $inc recursive the field value. $set set the field value. $push pushes an entry into an array. $rename Rename the field. etc ·
Parameter three: optional, two attributes. multi (matches all document updates, defaults to False only the first one) and Upsert (if no match is found, creates a, default false not created), Boolean.
*******************************************************************
db.< collection name >.pretty () a neatly displayed query document
db.< collection name >.find (). Sort ({property: 1/-1}) sorted by specified property
1 is the positive sequence,-1: Reverse
db.< collection name >.find (). Limit (n) Displays the first n data
db.< collection name >.find (). Skip (n) skips the first n data
Condition Lookup
db.< Collection Name >.find ({property: value}) Property = = value
db.< Collection name >.find ({attribute: {operator: value}})
Operator:
$lt less than
$lte less than or equal to
$GT Greater than
$gte greater than or equal to
$ne Not equal to
Use two operators for the same property
db.< Collection name >.find ({attribute: {operator 1:value, operator 2:value}})
such as:db.< collection name >.find ({age: {$gt: 6, $lt: 16}})
Simultaneous qualification of different attributes (two conditions at the same time)
db.< Collection name >.find ({attribute 1: {operator: value}, property 2: {operator: value}})
or condition
db.< Collection name >.find ({
$or: [
{property: Value},
{property: Value},
{Property: Value}
]
})
Using functions to filter data
db.< Collection name >.find ({
$where: function () {
Return True/false
}
})
MongoDB Common Commands