Skip to content

Instantly share code, notes, and snippets.

@jdnichollsc
Created January 13, 2016 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdnichollsc/ab1d0d2d4d2c1460ac54 to your computer and use it in GitHub Desktop.
Save jdnichollsc/ab1d0d2d4d2c1460ac54 to your computer and use it in GitHub Desktop.
//Copy the collection to prevent deleting data
db.nameCollection.copyTo('newNameCollection')
//See the collections in the database
db.getCollectionNames()
//See if exist duplicate data
db.orders.aggregate([
{
$group: {
_id: { extOrderId: "$extOrderId" },
uniqueIds: { $addToSet: "$_id" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
}
])
//Remove duplicate data (Run this code until cur is empty)
var cur = db.orders.aggregate([{ $group: { _id: { extOrderId: "$extOrderId" }, uniqueIds: { $addToSet: "$_id" }, count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }]);
while (cur.hasNext()) {
var doc = cur.next();
var index = 1;
while (index < doc.uniqueIds.length) {
db.orders.remove(doc.uniqueIds[index]);
index = index + 1;
}
}
//Compare the number of documents
db.orders14122015.distinct("extOrderId")
db.orders.count()
//See the Indexes of one collection
db.orders.getIndexes()
//Create the Index
db.orders.createIndex( {"extOrderId":1},{unique:true})
//Drop temporal collections
db['tempcollection'].drop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment