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

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 :) )


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}