Please, Stop Using MongoDB as a SQL Database
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
)
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





