浏览代码

if index already exists, dropIndex() and createIndex()

Shun Miyazawa 4 年之前
父节点
当前提交
4d3cf30175
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      packages/app/src/server/service/activity.ts

+ 6 - 0
packages/app/src/server/service/activity.ts

@@ -58,6 +58,12 @@ class ActivityService {
     const collection = mongoose.connection.collection('activities');
 
     try {
+      const indexes = await collection.getIndexes();
+      const isExistTargetField = Object.keys(indexes).includes('createdAt_1');
+      if (isExistTargetField) {
+        await collection.dropIndex('createdAt_1');
+      }
+
       // Set retention period only if activityExpirationSeconds is not null
       await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
     }