Created
January 13, 2016 22:56
-
-
Save jdnichollsc/ab1d0d2d4d2c1460ac54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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