Skip to content

Instantly share code, notes, and snippets.

@josemarcosrf
Last active October 26, 2023 22:43
Show Gist options
  • Save josemarcosrf/a23c184bcf6af202ed353a300acc9448 to your computer and use it in GitHub Desktop.
Save josemarcosrf/a23c184bcf6af202ed353a300acc9448 to your computer and use it in GitHub Desktop.
Mongo useful command examples; aggregations, dumps, restore, etc
projects=$(atlas projects list | jq -cr '.results[] | {id, name} | select(.name!="test-gpu-261023")')
for row in $projects; do
projectId=$(echo $row | jq -r '.id')
projectName=$(echo $row | jq -r '.name')
echo "Deleting serverless instance: $projectName (ID: $projectId)"
atlas serverless delete $projectName --projectId $projectId
done
for row in $projects; do
projectId=$(echo $row | jq -r '.id')
projectName=$(echo $row | jq -r '.name')
echo "Deleting PROJECT: $projectName (ID: $projectId)"
atlas projects delete $projectId
done

Counting how many records have the same name (example of aggregation by field value)

db.getCollection('docs').aggregate([
    {$match: {}},
    {$group: {'_id':"$name", num:{$sum:1} }},
    {$sort:{num:-1}}, { $limit : 50}
])

The opposite would be to count how many unique records exists for a given field:

db.getCollection('docs').distinct('name')

Check for field existance and matching regex, sorting and limiting

db.getCollection('docs_metadata').find({size: {$exists: true}, name: {$regex: /.+pdf/i}}).sort({size: -1}).limit(5)

Check count and average document size in a given collction

var maxSize = 1024;
var bigDocs = 0;
var avgSize = 0;
var count = 0;

db.getCollection('docs_metadata').find({}).forEach(
    function (doc) {
        count++;
        var docSize = Object.bsonsize(doc);
        avgSize += docSize;
        if (docSize >= maxSize) {
            bigDocs++;
            // print(doc._id + ' is ' + docSize + ' bytes');
        }
    }
)

print(`Found ${bigDocs} (out of ${count}) documents bigger than ${maxSize} bytes.\nAverage doc size: ${avgSize / count}`);

note: Answer from: https://stackoverflow.com/questions/4880874/how-do-i-create-a-mongodb-dump-of-my-database

Use gzip for taking backup of one collection and compressing the backup on the fly:

mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz

or with a date in the file name:

mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz

Update: Backup all collections of a database in a date folder. The files are gziped:

mongodump --db somedb --gzip --out /backups/`date +"%Y-%m-%d"`

Or for a single archive:

mongodump --db somedb --gzip --archive > dump_`date "+%Y-%m-%d"`.gz

Or when mongodb is running inside docker:

docker exec <CONTAINER> sh -c 'exec mongodump --db <SOME_DB> --gzip --archive' > dump_`date "+%Y-%m-%d"`.gz

From the above mongodump commands, to restore from an archive:

note: the = sign in all parameters and the quotes ' specially is the parameters has funny characters

mongorestore \
    --gzip \
    --db='<your-db-name>' \
    --archive=<db-dump-archive>.gz \
    --uri='<mongodb://your-mongo-uri>' \
    --ssl \
    --verbose

Or

mongorestore \
    --gzip \
    --db='<your-db-name>' \
    --archive=<db-dump-archive>.gz \
    --host='<mongo-host>' \
    --username='<your-usr>' \
    --password='<your-pwd>' \
    --ssl \
    --verbose

NOTE: To restore only one collection add: --nsInclude='<db-name>.<collection-name>' \

NOTE: If an error like the follwoing pops up:

(BadValue) Retryable writes are not supported. Please disable retryable writes by specifying "retrywrites=false" in the connection string or an equivalent driver specific config.

Add the option --writeConcern="{w:0}"

@josemarcosrf
Copy link
Author

josemarcosrf commented Oct 26, 2023

UPDATE:

It is now possible to manage Mongo Atlas with the atlas-cli

💡 installation

# List all projects filtering out a given project by name
atlas projects list | jq -c '.results[] | {id, name} | select(.name!="test-gpu-261023")'
# Delete serverless
atlas serverless delete <project-name> --projectId <project-id>
atlas projects delete <project-id>

Or for the non Atlas especific mongocli replace atlas with mongocli iam

💡 installation

e.g.:

# List all projects
mongocli iam projects list | jq '.results[] | {id, name}'
# ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment