Rather than a standard blog entry, which mostly remains unchanged after creation, this is going to be a living document of MongoDB tricks which may be useful to other developers because they are useful to me. These are all utilized within the mongosh utility.

Basic commands

Here are some basic commands which show the databases, the collections and do basic queries.

  • See databases:
    show dbs;
  • Connect to a database:
    use database;
  • See the collections (tables) in the current database:
    show collections;
  • Find all records in the collection:
    db.collection.find();
  • Find a record by a field:
    db.collection.find({field: 'value'});
Some more advanced commands
  • See the information about a collection, including indexes and validation schema, if any:
    db.getCollectionInfos({name: 'collection'});
Categories