跳到主要内容

Mongo-DB CheatSheet for Developers

Database Commands​

CommandDescription
createUserCreates a new user.
show dbsView all databases
use dbNameCreate a new or switch databases
dbView current Database
db.dropDatabase()Delete Database
usersInfoReturns information about the specified users.

Collection Commands​

CommandDescription
db.createCollection('collection_name')Create a collection named 'collection_name'
db.collection_name.drop()Drop a collection named 'collection_name'

Row(Document) Commands​

CommandDescription
db.collection_name.find()Show all Rows in a Collection
db.collection_name.find().pretty()Show all Rows in a Collection (Prettified)
db.collection_name.findOne({name: 'ritwik'})Find the first row matching the object
db.collection_name.insert({'name': 'Ritwik','lang': 'sql','member_since': 5 })Insert One Row
db.collection_name.insertMany([{'name': 'Ritwik','lang': 'sql','member_since': 5}, {'name': 'Rohan','lang': 'Python','member_since': 3},{'name': 'Lovish','lang': 'Java','member_since': 4}])Insert many Rows
db.collection_name.find({lang:'Python'})Search in a MongoDb Database
db.collection_name.find().limit(2)Limit the number of rows in output
db.collection_name.find().count()Count the number of rows in the output
db.collection_name.updateOne({name: 'Shubham'},{$set: {'name': 'Harry','lang': 'JavaScript','member_since': 51}},{upsert: true})Update a row
db.collection_name.update({name: 'Rohan'},{$inc:{member_since: 2}})Mongodb Increment Operator
db.collection_name.update({name: 'Rohan'},{$rename:{member_since: 'member'}})Mongodb Rename Operator
db.collection_name.remove({name: 'Harry'})Delete Row
db.collection_name.deleteOne({name: 'Harry'})Delete one Row
db.collection_name.deleteMany({lang: 'JavaScript'})Delete many Row

MongoDB Query Operators​

There are many query operators that can be used to compare and reference document fields.

Comparison Operators​

The following operators can be used in queries to compare values:

CommandDescription
$eqValues are equal
$neValues are not equal
$gtValue is greater than another value
$gteValue is greater than or equal to another value
$ltValue is less than another value
$lteValue is less than or equal to another value
$inValue is matched within an array

Logical Operators​

The following operators can logically compare multiple queries.

CommandDescription
$andReturns documents where both queries match
$orReturns documents where either query matches
$norReturns documents where both queries fail to match
$notReturns documents where the query does not match

Evaluation Operators​

The following operators assist in evaluating documents.

CommandDescription
$regexAllows the use of regular expressions when evaluating field values
$textPerforms a text search
$whereUses a JavaScript expression to match documents

MongoDB Update Operators​

There are many update operators that can be used during document updates.

Fields​

The following operators can be used to update fields:

CommandDescription
$currentDateSets the field value to the current date
$incIncrements the field value
$renameRenames the field
$setSets the value of a field
$unsetRemoves the field from the document

Array​

The following operators assist with updating arrays.

CommandDescription
$addToSetAdds distinct elements to an array
$popRemoves the first or last element of an array
$pullRemoves all elements from an array that match the query
$pushAdds an element to an array

MongoDB - Aggregation​

ExpressionDescription
$sumSums up the defined value from all documents in the collection.
$avgCalculates the average of all given values from all documents in the collection.
$minGets the minimum of the corresponding values from all documents in the collection.
$maxGets the maximum of the corresponding values from all documents in the collection.
$pushInserts the value to an array in the resulting document.
$addToSetInserts the value to an array in the resulting document but does not create duplicates.
$firstGets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.
$lastGets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.