Last active
April 20, 2016 13:43
-
-
Save junzis/0ace97b8d709efaa48d1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# a set of useful scripts for mongodb | |
- batch deleting collections | |
- extract subset using fast aggregation method | |
- export query to csv (quick and dirty) |
This file contains hidden or 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
var collectionNames = db.getCollectionNames(); | |
for(var i = 0, len = collectionNames.length; i < len ; i++){ | |
var collectionName = collectionNames[i]; | |
if(collectionName.indexOf('NAMESTARTS_') == 0){ | |
db[collectionName].drop() | |
} | |
} |
This file contains hidden or 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
mongodump --db database_name --collection collection_name --out - | gzip > dump_file_name.gz |
This file contains hidden or 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
mongoexport -d DB -c COLL --type=csv --out file_name.csv --fields f1,f2,f3 |
This file contains hidden or 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
db.getCollection('FULL_SET').aggregate([{ $match:{ FIELD:"VALUE"} }, { $out: "SUB_SET" }]); |
This file contains hidden or 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
db.collection.find().limit(1).sort({$natural:-1}) |
This file contains hidden or 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
db.COLLECTION_NAME.mapReduce( | |
function(){ | |
var lat = Math.floor(this["loc"]["lat"]); | |
var lon = Math.floor(this["loc"]["lng"]); | |
var key = lat + "_" + lon; | |
emit(key, {count: 1}) | |
}, | |
function(state, values){ | |
var result = {count: 0}; | |
values.forEach(function(value) { | |
result.count += value.count; | |
}); | |
return result; | |
}, | |
{out: "histogram"} | |
); |
This file contains hidden or 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
use admin | |
db.runCommand({renameCollection:"sourcedb.mycol",to:"targetdb.mycol"}) |
This file contains hidden or 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
mongoexport -d DBNAME -c COLLNAME --query "{'FIELD':'VLAUE'}" --type=csv --fields F1,F2,F3 --out out.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment