Commit cc91b3bd by christopher lee

Removed outdate index scripts, and added new scripts

parent 156456ec
print ("backpopulating author_username into contents collection");
var tot = db.users.count();
print ("found " + tot + " users to process...");
var cnt = 0;
db.users.find({}, {external_id:1, username:1}).forEach(function (doc) {
db.contents.update(
{author_id:doc["external_id"], author_username:{$exists:false}},
{$set:{author_username:doc["username"]}},
{multi:true}
);
cnt += 1;
if (cnt == tot) {
print("done!");
} else if (cnt % 1000 === 0) {
print("processed " + cnt + " records (" + parseInt((cnt/tot)*100) + "% complete)");
}
});
print ("backpopulating content with orphaned author ids");
db.contents.update({author_username:{$exists:false}}, {$set:{author_username:null}}, {multi:true});
print ("done!");
print ("backpopulating hierarchical sorting keys into contents collection");
var tot = db.contents.find({"_type":"Comment","sk":{$exists:false}}).count();
print ("found " + tot + " comments to process...");
var cnt = 0;
db.contents.find({"_type":"Comment","sk":{$exists:false}}).forEach(function (doc) {
var i, sort_ids;
if (typeof(doc.sk)==="undefined") {
if (typeof(doc.parent_ids)==="undefined") {
sort_ids = [];
} else {
sort_ids = doc.parent_ids.slice(0);
}
sort_ids.push(doc._id);
doc.sk = sort_ids.map(function (oid) {return oid.str}).join("-");
db.contents.save(doc);
}
cnt += 1;
if (cnt == tot) {
print("done!");
} else if (cnt % 1000 === 0) {
print("processed " + cnt + " records (" + parseInt((cnt/tot)*100) + "% complete)");
}
});
print ("creating index on new sorting keys...");
db.contents.ensureIndex({"sk":1})
print ("all done!");
db.contents.ensureIndex({ _type: 1, comment_thread_id: 1, author_id: 1, updated_at: 1 }, { background: true })
db.contents.ensureIndex({ comment_thread_id: 1, sk: 1 }, { background: true, sparse: true })
db.contents.ensureIndex({ comment_thread_id: 1, endorsed: 1 }, { background: true, sparse: true })
db.contents.ensureIndex({ _type: 1, course_id: 1, pinned: -1, created_at: -1 }, { background: true })
db.contents.dropIndex({ sk: 1 }) // the new one (created above) supersedes this
db.users.dropIndex({ external_id: 1 }) // drop the non-unique one
db.users.ensureIndex({ external_id: 1 }, { unique: true, background: true })
db.subscriptions.ensureIndex({ source_id: 1, source_type: 1 }, { background: true })
db.contents.ensureIndex({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1}, {background: true})
db.contents.ensureIndex({_type: 1, course_id: 1, pinned: -1, "votes.point": -1, created_at: -1}, {background: true})
db.contents.ensureIndex({commentable_id: 1}, {sparse: true, background: true})
print ("Adding thread_type to all comment threads where it does not yet exist\n");
db.contents.update(
{_type: "CommentThread", thread_type: {$exists: false}},
{$set: {thread_type: "discussion"}},
{multi: true}
);
printjson (db.runCommand({ getLastError: 1, w: "majority", wtimeout: 5000 } ));
print ("Add the new indexes for the context field");
db.contents.ensureIndex({ _type: 1, course_id: 1, context: 1, pinned: -1, created_at: -1 }, {background: true})
db.contents.ensureIndex({ _type: 1, commentable_id: 1, context: 1, pinned: -1, created_at: -1 }, {background: true})
print ("Adding context to all comment threads where it does not yet exist\n");
var bulk = db.contents.initializeUnorderedBulkOp();
bulk.find( {_type: "CommentThread", context: {$exists: false}} ).update( {$set: {context: "course"}} );
bulk.execute();
printjson (db.runCommand({ getLastError: 1, w: "majority", wtimeout: 5000 } ));
print ("removing fields 'sk' and 'author_username' from contents collection...");
db.contents.update({}, {$unset:{"sk":1, "author_username":1}}, { multi: true });
print ("removing index on contents.sk");
db.contents.dropIndex({"sk":1});
print ("all done!");
db.contents.ensureIndex({ sk: 1 }, { background: true, safe: true })
db.contents.dropIndex({ comment_thread_id: 1, updated_at: 1 })
db.contents.dropIndex({ comment_thread_id: 1, sk: 1 })
db.contents.dropIndex({ comment_thread_id: 1, endorsed: 1 })
db.contents.dropIndex({ _type: 1, course_id: 1, pinned: -1, created_at: -1 })
db.users.dropIndex({ external_id: 1 }) // drop the unique one
db.users.ensureIndex({ external_id: 1 }, { background: true })
db.subscriptions.dropIndex({ source_id: 1, source_type: 1 })
db.contents.dropIndex({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1})
db.contents.dropIndex({_type: 1, course_id: 1, pinned: -1, "votes.point": -1, created_at: -1})
db.contents.dropIndex({commentable_id: 1})
db.users.ensureIndex({email: 1}, {background: true})
print ("Removing thread_type from all comment threads\n");
db.contents.update(
{_type: "CommentThread"},
{$unset: {thread_type: ""}},
{multi: true}
);
printjson (db.runCommand({ getLastError: 1, w: "majority", wtimeout: 5000 } ));
print ("remove the indexes for the context field");
db.contents.dropIndex({ _type: 1, course_id: 1, context: 1, pinned: -1, created_at: -1 })
db.contents.dropIndex({ _type: 1, commentable_id: 1, context: 1, pinned: -1, created_at: -1 })
print ("Removing context from all comment threads\n");
db.contents.update(
{_type: "CommentThread"},
{$unset: {context: ""}},
{multi: true}
);
printjson (db.runCommand({ getLastError: 1, w: "majority", wtimeout: 5000 } ));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment