NoSQL Zone is brought to you in partnership with:

24 h Software developer interested in all types of technologies. Android, iOS & node.js are my strengths at the moment! Javier is a DZone MVB and is not an employee of DZone and has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Please, Stop Using MongoDB as a SQL Database

  • submit to reddit

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:},
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 Javier Manzano, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)