As part of our MongoDB Guide, we’ve compiled this cheat sheet of common and not-so-common MongoDB commands.
- MongoDB Cheat Sheet Show All Databases show dbs Show Current Database db Create Or Switch Database use acme Drop db.dropDatabase Create Collection db.createCollection('posts') Show Collections show collections Insert Row.
- To protect against SQL injection, clients can use MongoDB's language APIs. This way, all the input is simple value - commands cannot be injected. A Java example: collection.find(Filters.eq('key', 'input value')) The drawback is that you cannot easily test your filter. You cannot copy it.
Clone the repo and checkout the cheat-sheet branch; Download DocFX from here; Add DocFX to your system path or place the executable in the MongoDB.Entities Documentation folder; Change the current working directory to MongoDB.Entities Documentation folder; Run DocFX with the command docfx -serve; Open a browser and visit: http://localhost:8080. This article will help you build the famous Snake Game using python's turtle module. It explains every single section of the code to understand the logic.
(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)
Table of Contents
Pretty Print
Create Collection
Create Indexes
Create index
Create sparse index
Create compound index
Create geo index
Create partial index
Add and Delete Data
Add one data record
Add array (many) records
Delete data
Query MongoDB Documents
Search by attribute
Search by geolocation
Search greater or less than
Search not equal to
Search and return only certain fields
Search by regular expression
Find by elements in array
Replication and Sharding
Enable replication
Add shard
Show Server Memory
Aggregate functions
MapReduce
Create Collection
Create Indexes
Create index
Create sparse index
Create compound index
Create geo index
Create partial index
Add and Delete Data
Add one data record
Add array (many) records
Delete data
Query MongoDB Documents
Search by attribute
Search by geolocation
Search greater or less than
Search not equal to
Search and return only certain fields
Search by regular expression
Find by elements in array
Replication and Sharding
Enable replication
Add shard
Show Server Memory
Aggregate functions
MapReduce
Pretty Print
Add pretty() to the end of any search:
Create Collection
Create Indexes
Create index
The format is {attribute : sortOrder} where sort order is -1 (descending) or 1 (ascending).
Create sparse index
This does not index documents that do not have this attribute.
Create compound index
Create geo index
Create partial index
This indexes those documents that match the search criteria.
Add and Delete Data
Add one data record
Add array (many) records
Delete data
Query MongoDB Documents
Search by attribute
Search by geolocation
Search greater or less than
Search not equal to
Search and return only certain fields
This is called projection.
Show only the make field:
Show every field except the make field:
Search by regular expression
You can use the slash or quote marks as a delimiter.
Case insensitive:
Find by elements in array
This matches documents that contain all of these array elements:
Match on any element in the array:
Replication and Sharding
Mongodb Query Language Cheat Sheet
Enable replication
Connect to a config server then:
And then show replication status:
Add shard
Connect to query router (mongos) server. 27018 in this example is the port number of a shard server
Show Server Memory
Aggregate functions
This is the same as the SQL select count(*) from words group by word. This is the word count program.
MapReduce
You can do the same word count function using the map operation:
The cheat-sheet exists to showcase how things are done; the MongoDB.Entities way.
Choose a category on the left to start browsing.
If you're interested in contributing to the cheat-sheet section of the documentation, please send in your PR's to the
cheat-sheet
branch of the github repository.Mongodb Sql Injection Cheat Sheet
- Only add non-existing code
- Add your contribution to the appropriate category
- Create new categories if needed
- Only use the domain/context of a bookshop or library which would have entities such as:
- Author
- Book
- Publisher
- Editor
- Genre
- etc.
- Optionally add your name/github profile link right below your code like:
Contributed by: YourName
Mongodb Injection Cheat Sheet Excel
- Clone the repo and checkout the
cheat-sheet
branch - Download DocFX from here
- Add DocFX to your system path or place the executable in the
MongoDB.Entities > Documentation
folder - Change the current working directory to
MongoDB.Entities > Documentation
folder - Run DocFX with the command
docfx --serve
- Open a browser and visit:
http://localhost:8080
- Make your changes to the markdown files inside
MongoDB.Entities > Documentation > cheat-sheet
folder - To see your changes in the browser,
CTRL+C
and rundocfx --serve
again