Over a million developers have joined DZone.

How to Translate SQL Queries to MongoDB

DZone's Guide to

How to Translate SQL Queries to MongoDB

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

We've heard plenty of reasons one might transition from a relational database to a NoSQL solution like MongoDB, but there can be a learning curve. Developers looking to switch over to (or just do some side work with) MongoDB might be interested in Max Seiden's tips on how to translate SQL queries to MongoDB. In Seiden's case, MongoDB is a "log analytics engine" running alongside PostgreSQL, but his use case has still produced a number of useful techniques.

Specifically, Seiden runs through an assortment of SQL needs and demonstrates their usage in MongoDB. For each, he also gives a brief explanation of what's interesting about the translation and about the queries themselves. For example, he looks at the following topics:

  • Counts & Distincts
  • Counts with Predicates
  • Projections with Predicates
  • Group by remote IP with URLs and Predicates
  • Group by remote IP with Distinct URLs and Predicates

And Seiden even gathered some useful, real-world insights with this set-up. He was able to discover "petty attacks" on his website using a modified version of one of his "distinct" queries, for example.

There are, of course, drawbacks and caveats, but overall, Seiden's SQL-to-MongoDB examples provide a fantastic look at how the two database options relate to each other. Read the full article to see how it's all done.

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}