Maintaining MongoDB Databases

Introduction

Either use the mongo command line or RoboMongo or MongoChef. Don’t forget that you don’t necessarily have to open 27017 on the network to use it (ssh tunnel & use localhost:27017).

See what databases are available and choose one,

mongo
show databases
use <database name>

Searching Data

Search for a document containing foo in the users collection,

db.users.find({username: 'foo'});

and // makes it case-insensitive,

db.users.find({username: /foo/});

Updating Data

Reset the value for e.g. happyTimeout in the users collection on all the documents (multi true),

db.users.update({}, {$set: {happyTimeout : null}}, {multi : true})

Dropping a Database

Check the db names,

mongo
show dbs

Copy & nuke a DB entirely,

db.copyDatabase('dbname', 'dbcopy');
use dbname
db
db.dropDatabase();

or as one-liner from the shell,

mongo dbname --eval "db.dropDatabase();"

HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5 | CONTACT
Copyright © 2024 Pierre-Philipp Braun