Over a million developers have joined DZone.

Please, Stop Using MongoDB as a SQL Database

DZone's Guide to

Please, Stop Using MongoDB as a SQL Database

· Database Zone
Free Resource

Download the Guide to Open Source Database Selection: MySQL vs. MariaDB and see how the side-by-side comparison of must-have features will ease the journey. Brought to you in partnership with MariaDB.

I’ve read in numerous sites and blog posts some kind of tips or tutorials about MongoDB that includes the most SQL-ish structure that you wouldn’t do in MongoDB (of course not all situations fit this, but it’s a good approach in some cases)

I’m talking about embedded objects collections. In an usual SQL database you would do a JOIN to find all the objects that are connected to a simple row. We all have done that, but… In MongoDB, if it doesn’t mean content replication, this should be achieved throught embedded collections.

Take this example with mongoose and node.js:

var NotificationSchema = new Schema({
type: String,
message: String,
createdAt: {type:Date, default: Date.now},
read: {type: Boolean, default: false}
var UserSchema = new Schema({
username: String,
notifications: [NotificationSchema]

This is a real example that’s been used on production (obviously whithout some fields).

With this simple declaration, everytime you access User model you will retrieve notifications too. Of course if you want to send users over the net and don’t want to pass all of this you could use underscore’s pick to choose what to send or not (that was just a little tip for express users :) )

Interested in reducing database costs by moving from Oracle Enterprise to open source subscription?  Read the total cost of ownership (TCO) analysis. Brought to you in partnership with MariaDB.


Published at DZone with permission of Javier Manzano, DZone MVB. See the original article here.

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 }}