DEV Community

Cover image for How Its Made: MongoDB Compass Export to Ruby Language
rachelle palmer for MongoDB

Posted on

How Its Made: MongoDB Compass Export to Ruby Language

Looking for something truly useful for learning MongoDB? Imagine being able to construct a MongoDB query via the MongoDB Compass UI while exploring your data, then exporting that same query to the programming language of your choice.

Image description

In today's "How Its Made", MongoDB Engineer Neil Shweky explains how he recently built out this feature for Ruby.

Getting this functionality working required that Ruby be added as an output language to our BSON Transpilers project. As an aside, if you spent a lot of time in university (or just for fun) with compilers, parsers, and other fun functional-programming-related problems, this is a project for you.

Our BSON transpilers enable users to convert a MongoDB query from one programming language to another via a YAML template file that specifies how to generate code for the specific language in a variety of situations. For example, if the output language is Python and the input code is '3 in [1,2,3]' the following template is used to generate equivalent Ruby code:

InSyntaxTemplate:
(lhs, op, rhs) => {
  let str = '';
  if (op.includes('!') || op.includes('not')) {
    str = '!';
  }
  return${str}${rhs}.include?(${lhs})
}
Enter fullscreen mode Exit fullscreen mode

This template takes in an lhs value of 3 and a rhs value of [1,2,3]. The equivalent Ruby code that is then generated to test for array membership would be:

[1, 2, 3].include?(3)
Enter fullscreen mode Exit fullscreen mode

The first step in this process was to complete a series of tests which would evaluate a code statement from an input language and output it to Ruby. Then to take those learnings and complete the templates in the ruby template file.

Simple enough, right?

The most difficult part of this work was making decisions on what code is considered idiomatic to Ruby. The Ruby language offers so many ways to express the same concepts, however our goal was to choose the ideal way that would make sense to Ruby developers. As an example, assignment of a key/value pair to a Hash of { x: 1 } can be done multiple different ways:

  1. { x: 1 }
  2. { :x => 1 }
  3. { :'x' => 1 }
  4. { 'x': 1 }
  5. { 'x' => 1 }

Well...

Eagle-eyed Rubyists will note that the keys above vary between being Symbols and Strings so it's not technically "5 different ways of assigning exactly the same thing", but we chose this example to illustrate the flexibility the Ruby language offers. At the end of the day, we opted to use the last option, because we felt like this would "look right" to most Ruby developers.

Results in the Compass UI:
Image description

For future enhancements, we are thinking about adding Ruby as an input language. This would allow a user to convert queries written in Ruby to any of the other languages we have implemented such as Java, C#, Javascript, Python, with Golang and even Rust.

The next time you're exporting your query from within MongoDB Compass give Ruby a try and let us know what you think. If you're curious how all this works the repo for MongoDB Compass is available for exploration.

-- Rachelle, Product Lead for Developer Experience

P.S. Neil is also on twitter and devto

Top comments (0)