3

I like the schemaless architecture of Mongodb. That makes it so flexible. Yet there's certain requirements like model relationship and data validation that is needed which Mongoose ODM provides.

I've read a handful of SO threads where I found that Mongoose's performance in handling really complex document structure is not that good comparative to the native MongoDB driver, in my case NodeJS driver.

I dont want to loose the being-schemaless flexibility which makes it really great to change my structure whenever I want. But I want to use the model relationship.

Which practice is better : schemaless or using schema particularly in Mongo?

somnathbm
  • 649
  • 1
  • 9
  • 19

1 Answers1

1

Here are some ways to avoid some of the overhead that mongoose provides and a way to get access to native mongoDb commands for the NodeJs driver in your script.

You are able to get access to The mongodb.Db instance.

because of that you can get access to the db constructor for the Nodejs driver.

So I could do stuff like this in my node js/ mongoose file

mongoose.connection.db.dropCollection("collectionName", function(err, doc){
    if(err) console.log(err);
    console.log(doc);
})

Also if you don't want the overhead that mongoose provides with the extra properties on the object that is returned from queries you can use .lean(). If you use lean you get back plain javascript objects and not mongoose documents. Lean is much faster.

EXAMPLE::

Model.find().lean().exec(function (err, docs) {
  docs[0] instanceof mongoose.Document // false
});
jack blank
  • 5,073
  • 7
  • 41
  • 73