mongodb clone document code example

Example 1: mongodb clone database

# Dump db to a local folder
mongodump mongodb://user:pwd@localhost/old_name -o ./dump 

# Restore the db with the new name
mongorestore mongodb://user:pwd@localhost -d new_name ./dump/old_name

# Try this flag if you get an authentication error
--authenticationDatabase admin

Example 2: duplicated documens mongodb

db.users.aggregate([
    {$group: {
        _id: {email: "$email"},
        uniqueIds: {$addToSet: "$_id"},
        count: {$sum: 1}
        }
    },
    {$match: { 
        count: {"$gt": 1}
        }
    },
    {$sort: {
        count: -1
        }
    }
]);