-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttl_unique_indexcheck.js
48 lines (34 loc) · 1.22 KB
/
ttl_unique_indexcheck.js
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
// 检查当前集群中有没有 TTL 或 Unique 索引,做迁移前的检查
const databases = db.adminCommand({ listDatabases: 1 }).databases;
const ttlCollections = [];
const uniqueCollections = [];
databases.forEach(function(database) {
const dbName = database.name;
if (dbName !== 'admin' && dbName !== 'config' && dbName !== 'local') {
const currentDb = db.getSiblingDB(dbName);
const collections = currentDb.getCollectionNames();
collections.forEach(function(collectionName) {
const indexes = currentDb.getCollection(collectionName).getIndexes();
const ttlIndex = indexes.find(index => index.expireAfterSeconds !== undefined);
const uniqueIndexes = indexes.filter(index => index.unique);
if (ttlIndex) {
ttlCollections.push(`${currentDb.getName()}.${collectionName}`);
}
if (uniqueIndexes.length > 0) {
uniqueCollections.push(`${currentDb.getName()}.${collectionName}`);
}
});
}
});
if (ttlCollections.length > 0) {
print('TTL:');
ttlCollections.forEach(function(collection) {
print(collection);
});
}
if (uniqueCollections.length > 0) {
print('Unique:');
uniqueCollections.forEach(function(collection) {
print(collection);
});
}