use $regex instead of re.compile in mongo queries, so it uses indexes properly.  Maybe fixed in current mongo versions https://jira.mongodb.org/browse/SERVER-26991
diff --git a/Allura/allura/controllers/project.py b/Allura/allura/controllers/project.py
index 8ddb21d..f4d8c98 100644
--- a/Allura/allura/controllers/project.py
+++ b/Allura/allura/controllers/project.py
@@ -687,7 +687,7 @@
         if icon is not None and icon != b'':
             if self.neighborhood.icon:
                 self.neighborhood.icon.delete()
-                M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon')))
+                M.ProjectFile.query.remove(dict(project_id=c.project._id, category={'$regex': r'^icon'}))
             save_icon = c.project.save_icon(icon.filename, icon.file, content_type=icon.type)
             if save_icon:
                 M.AuditLog.log('update neighborhood icon')
diff --git a/Allura/allura/controllers/site_admin.py b/Allura/allura/controllers/site_admin.py
index 5b41bc9..0700902 100644
--- a/Allura/allura/controllers/site_admin.py
+++ b/Allura/allura/controllers/site_admin.py
@@ -542,9 +542,9 @@
         if state:
             query['state'] = state
         if task_name:
-            query['task_name'] = re.compile(re.escape(task_name))
+            query['task_name'] = {'$regex': re.escape(task_name)}
         if host:
-            query['process'] = re.compile(re.escape(host))
+            query['process'] = {'$regex': re.escape(host)}
 
         tasks = list(M.monq_model.MonQTask.query.find(query).sort('_id', -1))
         for task in tasks:
diff --git a/Allura/allura/controllers/trovecategories.py b/Allura/allura/controllers/trovecategories.py
index 1c0c14a..d94d60e 100644
--- a/Allura/allura/controllers/trovecategories.py
+++ b/Allura/allura/controllers/trovecategories.py
@@ -131,11 +131,11 @@
 
         if upper:
             trove_type = upper.fullpath.split(' :: ')[0]
-            fullpath_re = re.compile(fr'^{re.escape(trove_type)} :: ')  # e.g. scope within "Topic :: "
+            fullpath_re = fr'^{re.escape(trove_type)} :: '  # e.g. scope within "Topic :: "
         else:
             # no parent, so making a top-level.  Don't limit fullpath_re, so enforcing global uniqueness
-            fullpath_re = re.compile(r'')
-        oldcat = M.TroveCategory.query.get(shortname=shortname, fullpath=fullpath_re)
+            fullpath_re = r''
+        oldcat = M.TroveCategory.query.get(shortname=shortname, fullpath={'$regex': fullpath_re})
 
         if oldcat:
             raise TroveAdminException(
diff --git a/Allura/allura/ext/admin/admin_main.py b/Allura/allura/ext/admin/admin_main.py
index fc2f2ac..6e8232f 100644
--- a/Allura/allura/ext/admin/admin_main.py
+++ b/Allura/allura/ext/admin/admin_main.py
@@ -336,7 +336,7 @@
             c.project.removal = removal
             c.project.removal_changed_date = datetime.utcnow()
         if 'delete_icon' in kw:
-            M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon')))
+            M.ProjectFile.query.remove(dict(project_id=c.project._id, category={'$regex': r'^icon'}))
             c.project.set_tool_data('allura', icon_original_size=None, icon_sha256=None)
             M.AuditLog.log('remove project icon')
             g.post_event('project_updated')
@@ -415,7 +415,7 @@
 
         if icon is not None and icon != b'':
             if c.project.icon:
-                M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon')))
+                M.ProjectFile.query.remove(dict(project_id=c.project._id, category={'$regex': r'^icon'}))
             save_icon = c.project.save_icon(icon.filename, icon.file, content_type=icon.type)
             if not save_icon:
                 M.AuditLog.log('could not update project icon')
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 47defa5..d881632 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -567,8 +567,8 @@
         escaped_underscore = re.escape('_')  # changes in py3.x versions # https://docs.python.org/3/library/re.html#re.escape
         un = un.replace(escaped_underscore, '[-_]')
         un = un.replace(r'\-', '[-_]')
-        rex = re.compile('^' + un + '$')
-        return M.User.query.get(username=rex, disabled=False, pending=False)
+        rex = r'^' + un + '$'
+        return M.User.query.get(username={'$regex': rex}, disabled=False, pending=False)
 
     def set_password(self, user, old_password, new_password):
         if old_password is not None and not self.validate_password(user, old_password):
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index 33a675e..c302e24 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -161,7 +161,7 @@
 
     @property
     def children(self):
-        return sorted(self.query.find({'fullpath': re.compile('^' + re.escape(self.fullpath) + ' ::')}).all(),
+        return sorted(self.query.find({'fullpath': {'$regex': '^' + re.escape(self.fullpath) + ' ::'}}).all(),
                       key=lambda t: t.fullpath.lower())
 
     @property
diff --git a/ForgeDiscussion/forgediscussion/controllers/forum.py b/ForgeDiscussion/forgediscussion/controllers/forum.py
index 953d40d..064135c 100644
--- a/ForgeDiscussion/forgediscussion/controllers/forum.py
+++ b/ForgeDiscussion/forgediscussion/controllers/forum.py
@@ -136,7 +136,7 @@
                 user_id=c.user._id,
                 project_id=c.project._id,
                 app_config_id=c.app.config._id,
-                artifact_index_id=re.compile('^' + re.escape(forumthread_index_prefix)),
+                artifact_index_id={'$regex': '^' + re.escape(forumthread_index_prefix)},
             )).all()
             # get the ForumThread objects from the subscriptions
             thread_index_ids = [mbox.artifact_index_id for mbox in thread_mboxes]
diff --git a/ForgeUserStats/forgeuserstats/controllers/userstats.py b/ForgeUserStats/forgeuserstats/controllers/userstats.py
index 7efcb51..7df06f5 100644
--- a/ForgeUserStats/forgeuserstats/controllers/userstats.py
+++ b/ForgeUserStats/forgeuserstats/controllers/userstats.py
@@ -38,7 +38,7 @@
 
     @expose()
     def _lookup(self, category, *remainder):
-        cat = M.TroveCategory.query.get(shortname=category, fullpath=re.compile(r'^Topic :: '))
+        cat = M.TroveCategory.query.get(shortname=category, fullpath={'$regex': r'^Topic :: '})
         if not cat:
             raise exc.HTTPNotFound
         return ForgeUserStatsCatController(category=cat), remainder