MongoDB Cheat Sheet

As frequent-but-not-every-day-one-needs-these-command MongoDB users, we, obviously, can use a cheat sheet. Here is the start of one.

Rename a collection

This is done via an admin command, using the full path (i.e. “database.collection”). Destructive, if _NewCollection_ exists (when using dropTarget: true).

db.adminCommand( { renameCollection:"_database_._OldCollection_", to: "_database_._NewCollection_", dropTarget: true } )

Count distinct values with a condition

In this example the count of distinct values for pmid is shown, vor all samples having a partial match of “CVCL” in their cellosaurusid.

db.samples.distinct("pmid",{"cellosaurusid":{$regex:/CVCL/i}}).length

2018-04-10