Quellcode durchsuchen

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

Fix slow query for user with confirmed emails

GitOrigin-RevId: aec3429073bf45e192ca8b0b4a2ac90ebf22b510
Alasdair Smith vor 6 Jahren
Ursprung
Commit
69521047e2

+ 7 - 2
services/web/app/src/Features/User/UserGetter.js

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

+ 24 - 0
services/web/test/unit/src/User/UserGetterTests.js

@@ -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() {
     it('should find users by list of v1 ids', function(done) {
       const v1Ids = [501]