Summary: Indexes created inside mongoDB compass are not used by Node driver. Using serverless.
Explanation/proof:
Running the following command in MongoDB compass gets me the following result:
db.getCollection('users').getIndexKeys()
[
{ _id: 1 },
{ email: 1 },
{ oauthId: 1 },
{ _fts: 'text', _ftsx: 1 },
{ amplify_id: 1 }
]
I filter on the amplify_id field for each operation, which looks just like this:
mongodb.collection('users').find(
{ amplify_id: "123" },
)
And when I use the “explain” keyword on a “find” operation in my NODE script, the result is this:
await mongodb.collection('users').find(
{ amplify_id },
).explain()
totalDocsExamined: 1964,
However, when I do this in MongoDB Compass, I get 1 doc examined, as expected. Same query.
Node recognizes that an index exists:
await mongodb.collection<MongoUser>('users').indexes();
{
v: 2,
key: { amplify_id: 1 },
name: 'amplify_id_1',
unique: true,
sparse: true
}
…but does not use it.
This index was created in MongoDB compass. And sure enough, when I delete the index, then re-create it inside Node or with the mongo shell, it works:
totalDocsExamined: 1,
This is a huge problem and and can easily lead to unexpectedly high RPUs (billing going through the roof) and slow performance, and is not straightforward to detect.
I pointed out another bug with the Node driver not using TEXT indexes. The workaround was the same; create the index IN NODE, not in MongoDB Compass.
I’ll add that I really enjoy working in the Compass and it has been just fine for doing operations on data, but I won’t be using it anymore for deeper operations like creating indexes