Performance Difference in Mongoose vs MongoDB Native Driver

bugwheels94
3 min readMar 5, 2018

--

I am going to start a new project in which I will use schemaless database and when I was doing freecodecamp, I used Mongoose because I was new to this world. I faced some difficulties using it’s documentation and learned one more layer over already given one thing. This time, I have done partially awesome MongoDB course and I want to try this out. But I am not sure whether it is worth it or not! Stackoverflow Answers did not help that much as they told what I knew already.

Since, the syntax is easy for both to be honest. I took performance as a deciding factor. I decided to take the matter into my hands and run a benchmark on these two pieces of software myself.

I created a collection of 1 million records with the structure

{ 
_id: ObjectID,
subrecord: ObjectID // for reference, usual case in real DB
name: String,
job: String,
company: String
}

Here is results of running different benchmarks on different types of query under different scenarios

READ

findOne

When I did not use index and search by strings then the results were pretty close and nothing is clear

Mongoose x 21.45 ops/sec ±6.11% (53 runs sampled)
MongoDB x 22.33 ops/sec ±6.47% (55 runs sampled)
The Query is {'name': 'bugwheels500000'}

Same goes when I used two parameter instead of one

Mongoose x 11.86 ops/sec ±6.05% (57 runs sampled)
MongoDB x 12.73 ops/sec ±1.59% (61 runs sampled)
Fastest is MongoDB
Query: {name: 'bugwheels94544', job: 'Useless'}

Things got little interesting when I used indexes for searching where MongoDB is significantly faster

Mongoose x 1,929 ops/sec ±9.70% (66 runs sampled)
MongoDB x 4,269 ops/sec ±9.41% (70 runs sampled)
Query: {_id: ‘5a9cf6860acccf5de0cf948a’ }

Same pattern with text index search

Mongoose x 1,427 ops/sec ±9.62% (65 runs sampled)
MongoDB x 4,725 ops/sec ±18.34% (68 runs sampled)
Query: {name: {$text :{ $search: 'bugwheels500000'}}}

find

Again, with indexed search MongoDB is pretty good. Please don’t doubt that I have used Mongoose wrongly and it is not using index. If that would be the case, then mongoose would be same as “Single Parameter Query” in all 3 columns. You never doubted? Leave it then.

The Numbers for basic string search:

Mongoose x 2.24 ops/sec ±4.32% (15 runs sampled)
MongoDB x 2.25 ops/sec ±3.16% (16 runs sampled)
Query: {'name': 'bugwheels500000'}

With _id index

Mongoose x 2,238 ops/sec ±10.68% (70 runs sampled) 
MongoDB x 4,907 ops/sec ±9.31% (70 runs sampled)
Query: {_id: '5a9cf6860acccf5de0cf948a' }

With text index

Mongoose x 1,427 ops/sec ±9.62% (65 runs sampled)
MongoDB x 4,725 ops/sec ±18.34% (68 runs sampled)
Query: {name: {$text :{ $search: 'bugwheels500000'}}}

CREATE

Creating records is also very common operation so let’s look into this too. I will insert the following documents in following benchmarks

{
name: 'bugwheels',
interest: 'Not Many',
job: 'Useless'
}

insert

Insert will only insert one record at one time. Looks like there is negligible difference between insertion whether any additional index is set or not

The results are

With text index on name field

Mongoose x 1,004 ops/sec ±19.87% (64 runs sampled)
MongoDB x 2,589 ops/sec ±8.24% (66 runs sampled)

Without any user defined index

Mongoose x 1,053 ops/sec ±7.50% (71 runs sampled)
MongoDB x 2,452 ops/sec ±8.98% (63 runs sampled)

Pretty same for both! Looks like my effort for creating index wasted. But, it was one liner anyways.

insertMany

This time, I have inserted 100 records at one time and this should cover most bulk insert options. Results are

With text index

Mongoose x 118 ops/sec ±10.07% (69 runs sampled)
MongoDB x 252 ops/sec ±9.27% (47 runs sampled)

Without text index

Mongoose x 126 ops/sec ±9.93% (64 runs sampled)
MongoDB x 279 ops/sec ±8.67% (47 runs sampled)

I am leaving this article here without doing the UD of CRUD and I will also leave MongooseJS :| and use Native Driver as using them is not hard at all. The source code for running the test is here: Source Code.

I really hope that you liked my efforts. Let me know and who knows maybe I will also do update, delete and aggregate(join) too. I am always open for accepting tiniest mistakes so let me know those too. Thanks!

--

--

bugwheels94

I have a very keen interest in Computers. I practice few programming languages on a regular routine