Skip to content

Instantly share code, notes, and snippets.

@serpent403
serpent403 / mongo_export.sh
Last active January 2, 2016 00:49
Exports all the collections from a given database.
#!/bin/bash
db=mydatabase
out_dir="./"
tmp_file="fadlfhsdofheinwvw.js"
echo "print('_ ' + db.getCollectionNames())" > $tmp_file
cols=`mongo $db $tmp_file | grep '_' | awk '{print $2}' | tr ',' ' '`
for c in $cols
do
@serpent403
serpent403 / mongo_import.sh
Last active January 2, 2016 00:49
Imports all bson files present in a directory into the given database. Name of bson file must match the corresponding collection name. Place this script in the directory where your bson files are present and run it.
#!/bin/bash
host=localhost
db=mydatabase
for bson_file in `ls *.bson`
do
echo "importing $bson_file"
collection=$(echo "${bson_file}" | cut -d'.' -f1)
bson_path="$(pwd)/$bson_file"
@serpent403
serpent403 / mongo_commands.sh
Last active January 2, 2016 00:49
Handy MongoDB Commands
# mongo data export and import
mongoexport --host 10.xx.10.xx --db mydatabase --collection colln > colln.bson
mongoimport --host localhost --db mydatabase --collection colln colln.bson
@serpent403
serpent403 / linux_commands.sh
Last active January 1, 2016 23:29
Handy Linux Commands
# String replacement across multiple files
# '' -e done for osx
grep -rl string1 * | xargs sed -i '' -e 's/string1/string2/g'
# Search (options : recursive, case insensitive, line number, directories to exclude)
grep -r -i -n --exclude-dir={log,tmp,config,spec} 'search-string' *
@serpent403
serpent403 / map_reduce_embedded.js
Last active December 28, 2015 16:49
Map Reduce Example
use tutorial
db.carts.insert({ cust_id: "A123", amount: 25, status: "A", items: [ { sku: "mmm", qty: 5, price: 2.5 }, { sku: "nnn", qty: 5, price: 2.5 } ] })
db.carts.insert({ cust_id: "A123", amount: 50, status: "A", items: [ { sku: "mmm", qty: 10, price: 5 } ] })
db.carts.insert({ cust_id: "B212", amount: 30, status: "A", items: [ { sku: "nnn", qty: 2, price: 5 }, { sku: "kkk", qty: 1, price: 20 } ] })
db.carts.insert({ cust_id: "A123", amount: 90, status: "D", items: [ { sku: "mmm", qty: 10, price: 6 }, { sku: "kkk", qty: 3, price: 10 } ] })
var map = function() {
for(var idx = 0; idx < this.items.length; idx++) {
var key = this.items[idx].sku;
@serpent403
serpent403 / map_reduce_sum.js
Last active December 28, 2015 15:59
Map Reduce Example
use tutorial
db.orders.insert({ cust_id: "A123", amount: 500, status: "A" })
db.orders.insert({ cust_id: "A123", amount: 250, status: "A" })
db.orders.insert({ cust_id: "B212", amount: 200, status: "A" })
db.orders.insert({ cust_id: "A123", amount: 300, status: "D" })
// Calculate the sum of all “amount”s for a unique “cust_id”
@serpent403
serpent403 / mapreduce-example.js
Last active December 23, 2015 22:29
Mongodb Map Reduce Example
// Fetch average score and last answer grouped by visit_id field
db.feedbacks.insert({ "_id" : ObjectId("52419f0ae4b02f8c625a7ff1"), "feedback_question_id" : "52417f81e4b0b6ec25fdc988", "rater_id" : "506003b7047e9d38ee5b32ad", "merchant_id" : "52416008e4b02f4f07f78e53", "visit_id" : "52419c4ee4b07fbefee8803a", "score" : 4, "updated_at" : ISODate("2013-09-24T14:17:46Z"), "created_at" : ISODate("2013-09-24T14:17:46Z") });
db.feedbacks.insert({ "_id" : ObjectId("52419f0ae4b02f8c625a7ff2"), "feedback_question_id" : "52417f88e4b0b6ec25fdc98a", "rater_id" : "506003b7047e9d38ee5b32ad", "merchant_id" : "52416008e4b02f4f07f78e53", "visit_id" : "52419c4ee4b07fbefee8803a", "score" : 4, "updated_at" : ISODate("2013-09-24T14:17:46Z"), "created_at" : ISODate("2013-09-24T14:17:46Z") });
db.feedbacks.insert({ "_id" : ObjectId("52419f0ae4b02f8c625a7ff3"), "feedback_question_id" : "52417f8de4b091d72db45521", "rater_id" : "506003b7047e9d38ee5b32ad", "merchant_id" : "52416008e4b02f4f07f78e53", "visit_id" : "52419c4ee4b07fbefee8