1

I am using Mongoose for the first time in an application and I am observing much slower find() or count() operations compared to the raw MongoDB driver. I am trying to understand why. Here are the NodeJS npms used:

            "mongodb": "^2.2.33",
            "mongodb-autoincrement": "^1.0.1",
            "mongoose": "^4.12.5",
            "mongoose-delete": "^0.4.0",
            "mongoose-sequence": "^4.0.1",
            "mongoose-type-email": "^1.0.5",
            "mongoose-type-url": "^1.0.2",

And one of my indexes for this schema is:

contactModel.index({
    _owner: 1,
    email: 1
})

In a collection with 4.8M documents, Mongoose takes from 2s to 16s when querying like so:

contactModel.find({
    _owner: 'FFFFFFFFFFFFFFFFFFFFFFFF',
    email: 'johndoe@example.com'
})

And the Mongoose debug shows this raw query:

Mongoose: contact.find({ email: { '$eq': 'johndoe@example.com' }, _owner: { '$eq': ObjectId("FFFFFFFFFFFFFFFFFFFFFFFF") }, deleted: { '$ne': true } }, { skip: 0, limit: 1, fields: { name: true, email: true, lifecycle: true, properties: true, subscribed: true, created: true, modified: true, _id: true, _sqlid: true, _owner: true } })

But with MongoDB, it's under 100ms:

db.contact.find({
    _owner: ObjectId('FFFFFFFFFFFFFFFFFFFFFFFF'),
    email: 'johndoe@example.com'
})

I know that Mongoose has to apply schema defaults, convert strings to ObjectIds and such when applying the schema but the time difference is too much. Could it be a problem induced by one of the plugins used?

Can anyone help?

Maxime Asselin
  • 484
  • 1
  • 6
  • 14
  • What do you get as the raw query being run by mongoose when you have the debug flag set? – Alistair Nelson Nov 03 '17 at 14:18
  • I've edited the question, thanks. – Maxime Asselin Nov 03 '17 at 14:39
  • What if you test the exact raw query you're showing from Mongoose debug? That would be the equivalent test. – JohnnyHK Nov 03 '17 at 14:47
  • I tried with and without the deleted filter and that is the one affecting the speed since there are no index with _owner, email, deleted. I will try to find a better way to declare my indexes so that they always include deleted since the mongoose-delete plugin adds that to all finds. – Maxime Asselin Nov 03 '17 at 15:22
  • Question noted. Mongoose is very slow compared to raw query. Let me do a read, write bench-marking with raw mongo and mongoose mongo and will update the answer. – shijin Feb 13 '18 at 11:28
  • https://github.com/mongodb/node-mongodb-native Connect mongo using native mongo. Its not elaborate as mongoose,but will do the job in a more optimised way. – shijin Feb 13 '18 at 11:34

0 Answers0