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?