diff --git a/bin/scripts/add_indexes.js b/bin/scripts/add_indexes.js index af9c59eb24f..f257f0ef0ee 100644 --- a/bin/scripts/add_indexes.js +++ b/bin/scripts/add_indexes.js @@ -29,8 +29,6 @@ pluginManager.dbConnection().then((countlyDb) => { () => countlyDb.collection('app_users' + app._id).ensureIndex({"tsd": 1}, { background: true }, cb), () => countlyDb.collection('app_users' + app._id).ensureIndex({"did": 1}, { background: true }, cb), () => countlyDb.collection('app_users' + app._id).dropIndex("lac_1_ls_1", cb), - () => countlyDb.collection('app_user_merges' + app._id).ensureIndex({cd: 1}, {expireAfterSeconds: 60 * 60 * 3, background: true}, cb), - () => countlyDb.collection('metric_changes' + app._id).ensureIndex({ts: -1}, { background: true }, cb), () => countlyDb.collection('metric_changes' + app._id).ensureIndex({ts: 1, "cc.o": 1}, { background: true }, cb), () => countlyDb.collection('metric_changes' + app._id).ensureIndex({uid: 1}, { background: true }, cb) ]; diff --git a/bin/scripts/data-cleanup/delete_old_drill_events.js b/bin/scripts/data-cleanup/delete_old_drill_events.js new file mode 100644 index 00000000000..78f66efde24 --- /dev/null +++ b/bin/scripts/data-cleanup/delete_old_drill_events.js @@ -0,0 +1,66 @@ +/** + * MongoDB script to delete all collections from a database + * that start with "drill_events" string, but excluding the exact "drill_events" collection + * + * + * Server: MongoDB + * Path: any + * Command: mongosh -u uname -p 'password' --authenticationDatabase admin delete_old_drill_events.js + */ + +/* global db, print, quit */ + +// Set the database name +const dbName = "countly_drill"; + +// Set to true for dry run, false to actually delete collections +const dryRun = true; + +print(`Using database: ${dbName}`); +var cly = db; +// If we need to switch to a different database +if (db.getName() !== dbName) { + cly = db.getSiblingDB(dbName); +} + +console.log(`Operating on database: ${dbName}`); + +// Get all collection names that start with "drill_events" but are not exactly "drill_events" +const collections = cly.getCollectionInfos().map(info => info.name).filter(collName => + collName.startsWith("drill_events") && collName !== "drill_events" +); + +// Print the collections that will be deleted +print("Collections to be deleted:"); +collections.forEach(collName => print(`- ${collName}`)); + +// Ask for confirmation +if (collections.length === 0) { + print("No matching collections found to delete."); + quit(); +} + +print(`\nFound ${collections.length} collections to delete.`); + + +if (dryRun) { + print("Dry run mode is enabled. No collections will be deleted."); + print("To proceed with deletion, set 'dryRun' to false in the script."); + quit(); +} + +// Delete each collection +let deletedCount = 0; +collections.forEach(collName => { + try { + cly[collName].drop(); + print(`Dropped collection: ${collName}`); + deletedCount++; + } + catch (error) { + print(`Error dropping collection ${collName}: ${error}`); + } +}); + +// Print summary +print(`\nOperation completed. Deleted ${deletedCount} out of ${collections.length} collections.`); \ No newline at end of file diff --git a/bin/scripts/sharding/sharding.js b/bin/scripts/sharding/sharding.js index 9ed8bcb9cd8..6635ce2207d 100644 --- a/bin/scripts/sharding/sharding.js +++ b/bin/scripts/sharding/sharding.js @@ -2,13 +2,17 @@ * Sharding Countly collections when DB requires authentication, provide it to authDB.auth command in the code * Server: mongodb * Path: any -* Command: mongosh < sharding.js +* Command: mongosh -u uname -p 'password' --authenticationDatabase admin sharding.js */ -/* global Mongo, print, printjson */ -var COUNTLY_DRILL = 'countly_drill', - COUNTLY = 'countly', - COUNT_TO_SHARD = 100000; +/* global db, print, printjson */ + +// Set countly_drill database name +const COUNTLY_DRILL = 'countly_drill'; +// Set countly database name +const COUNTLY = 'countly'; +// Set the threshold for sharding collections +const COUNT_TO_SHARD = 100000; var EXCEPTIONS = [ /^system\./, @@ -27,14 +31,8 @@ var COUNTLY_TO_SHARD = [ "feedback", ]; -var conn = new Mongo(), - authDB = conn.getDB('admin'); - -// need to update this info -authDB.auth('', ''); - -var cly = conn.getDB(COUNTLY), - drill = conn.getDB(COUNTLY_DRILL); +var cly = db.getSiblingDB(COUNTLY), + drill = db.getSiblingDB(COUNTLY_DRILL); var clyCollections = cly.getCollectionNames(), collections = clyCollections.concat(drill.getCollectionNames()), check = []; @@ -61,8 +59,7 @@ check.forEach(function(c) { var db = clyCollections.indexOf(c) === -1 ? drill : cly, dbName = clyCollections.indexOf(c) === -1 ? COUNTLY_DRILL : COUNTLY, count = db[c].count(), - capped = db[c].stats()['capped'], - status = db[c].getShardVersion().ok; + capped = db[c].stats()['capped']; COUNTLY_TO_SHARD.some((e) => { if (c.indexOf(e) == 0) { @@ -71,7 +68,7 @@ check.forEach(function(c) { } }); - if (!capped && count > COUNT_TO_SHARD && !status && !exceptional) { + if (!capped && count > COUNT_TO_SHARD && !exceptional) { print('Creating hashed index & enabling sharding for collection "' + c + '"... '); db.getCollection(c).createIndex({ _id: 'hashed' });