Updated 2020-06-18: added database migration work
MongoDB GUI
Although I prefer the command line, for databases, CRUD via a GUI is really convenient. Unfortunately MongoDB has no good tool like phpMyAdmin. After trying many tools, I finally chose Robo3t.

CRUD
Basic CRUD is definitely the most used in work. Although work uses PyMongo, here I only list the Mongo shell operations, which are basically similar to PyMongo’s.
1 | // insert |
Building Indexes
- View existing indexes:
db.xxx.getIndexKeys()ordb.xxx.getIndexes()
MongoDB also uses B+ trees for indexes, so after adding an index, range queries get decent efficiency, while many random point queries are a bit slower.
The detailed official documentation is here: MongoDB Indexes.
I’ll pick a few that were new knowledge for me, but please always defer to the official docs. If there are any errors, please point them out and I’ll update promptly:
- When MongoDB creates a collection, it automatically adds an index for the
_idfield. - For single-field indexes, the sort direction doesn’t matter — ascending or descending, MongoDB will use it when querying. For multi-field indexes, they are organized by the field order used when creating the index and each field’s specified direction; if the query order differs from the index organization order, the index becomes ineffective.
- Partial indexes (version 3.2+): only entries meeting certain conditions are recorded by the index.
- Sparse indexes: only entries where a certain field exists are recorded by the index. The official docs recommend implementing this feature with partial indexes instead.
- TTL indexes: the indexed field can only be a Date type or an array of Date data; as soon as one Date value reaches its expiration, the record is deleted.
Using Explain
In MySQL, explain is definitely a good helper for optimizing queries — most importantly checking whether the inner search
used an index. Here is the official explain usage,
and a more detailed explain usage.
For now I only look at the stage in winningPlan; if it’s IXSCAN I’m basically at ease.
That doesn’t mean other fields are unimportant; if I encounter more in the future I’ll update this blog.
Javascript and the Mongo shell
In the Mongo shell you can use Javascript code, but the Mongo shell is really hard to use.
I suggest adding a file in Robo3t to run it. Below is a simple piece of code using Javascript
to simulate a join operation (run it — it should be easy to understand; the point is that Javascript code can be used):
1 | db.example.insert({'name': 'a', 'code': 1}) |
Some Uses of Aggregate
Many operations in MongoDB cannot be done with a single statement;
aggregateis suitable for simple computations that return results.
Official Aggregation documentation
Simulating Join ($lookup)
The $lookup operation was added in MongoDB versions after 3.2 — official Lookup documentation —
similar to MySQL’s join operation.
lookup must be used with the aggregate statement — Example
1 | // achieves the same effect as the js code above |
Simulating Group by
The official documentation explains it in detail; groupby is also the first example of the Pipeline.
Version-Related Problems I Encountered (updated irregularly)
** View the database version: db.version(); **
aggregate and Mongo version problems
Different versions of the MongoDB shell and MongoDB server will raise warnings, and aggregate statements
may also execute incorrectly.
1 | MongoDB shell version v4.0.2 |
I also encountered the error below during execution. If you also meet such a TypeError, you should consider using the
original version of the MongoDB shell on the server.
1 | > db.product.aggregate([{$group: {_id: "$p_id",count: { $sum: 1 }},},{$sort: {count: 1,},} ]) |
$ne, $eq version problems
$eq doesn’t exist in versions before 3.0. To achieve the effect of {<field>: { $eq: <value> }},
you can only use {"field": {'$not': {'$ne': <value>}}}. Honestly, using it this way is still
a bit mechanical.
$lookup version problems
As said above, lookup is only supported after 3.2; on versions before 3.2 you can only join manually!!!
explain version problems
After 3.0:
You can use
db.comment.explain().aggregate([...])You can usedb.comment.explain().find({...})
2.6 ~ 3.0:
To view explain information for db.comment.aggregate, use it like this:
1 | db.comment.aggregate([ |
To use explain with db.comment.find: do this:
db.admin.find({...}).explain()
Before 2.6:
aggregatecannot explain. To use explain with db.comment.find: do this:db.admin.find({...}).explain()
Really, upgrading MongoDB is quite necessary. Maybe many people think it’s easy for me to say. But trying a smooth database migration should also count as part of the job; what matters is getting the migration process and master/standby right, able to recover instantly.
Data migration work
MongoDB import/export work requires downloading some Database Tools; just choose the download for your system version.

1 | # export data to users.json |
MongoDB transactions
Only supported after 4.0. The company doesn’t use such a new version, nor MongoDB transactions. When I really use them I’ll share with everyone.