Merge pull request #3275 from overleaf/as-fix-confirmed-emails-query

Fix slow query for user with confirmed emails

GitOrigin-RevId: aec3429073bf45e192ca8b0b4a2ac90ebf22b510
This commit is contained in:
Alasdair Smith 2020-10-12 11:25:33 +01:00 committed by Copybot
parent 941241af54
commit 69521047e2
2 changed files with 31 additions and 2 deletions

View file

@ -97,13 +97,18 @@ const UserGetter = {
callback = projection callback = projection
projection = {} projection = {}
} }
// $exists: true MUST be set to use the partial index
const query = { const query = {
'emails.email': { $in: emails }, // use the index on emails.email
emails: { emails: {
$exists: true, $exists: true,
$elemMatch: { email: { $in: emails }, confirmedAt: { $exists: true } } $elemMatch: {
email: { $in: emails },
confirmedAt: { $exists: true }
} }
} }
}
db.users.find(query, { projection }).toArray(callback) db.users.find(query, { projection }).toArray(callback)
}, },

View file

@ -380,6 +380,30 @@ describe('UserGetter', function() {
}) })
}) })
describe('getUsersByAnyConfirmedEmail', function() {
it('should find users by confirmed email', function(done) {
const emails = ['confirmed@example.com']
this.UserGetter.getUsersByAnyConfirmedEmail(emails, (error, users) => {
expect(error).to.not.exist
expect(this.find).to.be.calledOnceWith(
{
'emails.email': { $in: emails }, // use the index on emails.email
emails: {
$exists: true,
$elemMatch: {
email: { $in: emails },
confirmedAt: { $exists: true }
}
}
},
{ projection: {} }
)
done()
})
})
})
describe('getUsersByV1Id', function() { describe('getUsersByV1Id', function() {
it('should find users by list of v1 ids', function(done) { it('should find users by list of v1 ids', function(done) {
const v1Ids = [501] const v1Ids = [501]