-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-share-token-index.js
More file actions
50 lines (41 loc) · 1.24 KB
/
fix-share-token-index.js
File metadata and controls
50 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const mongoose = require('mongoose');
require('dotenv').config();
async function fixShareTokenIndex() {
try {
// Connect to MongoDB
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Connected to MongoDB
const db = mongoose.connection.db;
const collection = db.collection('projects');
// Drop the existing shareToken index
try {
await collection.dropIndex('shareToken_1');
// Dropped existing shareToken index
} catch (error) {
// No existing shareToken index to drop
}
// Create the new sparse index
await collection.createIndex({ shareToken: 1 }, { sparse: true });
// Created new sparse shareToken index
// Verify the index was created
const indexes = await collection.indexes();
const shareTokenIndex = indexes.find(index =>
index.key && index.key.shareToken === 1
);
if (shareTokenIndex) {
// ShareToken index verified
} else {
// ShareToken index not found
}
// Migration completed successfully
} catch (error) {
// Error fixing shareToken index
} finally {
await mongoose.connection.close();
// Disconnected from MongoDB
}
}
fixShareTokenIndex();