Much DBA work is done in scripts for testing in dev and promotion up the quality stack. How can we make that easier in Mongo?
The Mongo shell is great and there are hacks to make it even better like mongo-hacker, but what about script writing? You can use two file types: Javacript or text. Both have advantages, but only one has standard syntax highlighting to make script writing easier. In IntelliJ, we can fix that.
File types: pros and cons
Block and inline comments
Reasonable syntax highlighting guesses at dotted objects, object keys and values
No Mongo specific syntax highlighting
No Mongo specific context appropriate command and operator suggestions
Keyword completion will show many things that are not appropriate - will break the script
You can pipe or redirect a text file to the mongo shell like this:
Full shell command access
Exactly the same syntax as working in the shell - same double quoting, etc.
Syntax highlighting for keywords and commands
Keyword completion that includes only Mongo commands and operators
Create a new .mongo file association
Open Preferences: ⌘, (Menu: IntelliJ IDEA -> Preferences…)
In the Preferences dialog left sidebar, show the Editor -> File Types page
Under Recognized File Types, click the ➕ (Add) button
Fill in the dialog as below, except for keywords which we’ll do next
There are four tabs for keywords, differentiated by highlight color.
Standard entry is one keyword at a time. With over a hundred operators alone, this is far too tedious to do by hand. There is a better way, described next. For now, click OK on the Edit File Type dialog.
In the Preferences dialog, under Registered Patterns, add ‘*.mongo’.
Click Apply and OK on the Preferences dialog.
I waded through the MongoDB 3.0 manual and collected all the commands and operators, sorted and de-duplicated them, and added them to the .mongo filetypes xml files.
Open ~/Library/Preferences/IntelliJIdea14/filetypes/Mongo Shell.xml and replace the contents with the xml at the bottom of this post.
Restart IntelliJ and you should have syntax highlighting for *.mongo files.
Adjusting syntax highlighting
For all manually configured file types, there is one highlighting scheme; a separate color for keywords on tabs 1, 2, 3, 4. You can view and edit these color by opening Preferences and selecting Editor -> Colors and Fonts -> Custom. the color associations are for Keyword1, Keyword2, Keyword3, Keyword4.