Menu
#Collections and databases
Collections are made on first use, and databases by the first write to them. These commands list them, remove them, and shrink the log.
listCollections, listDatabases, drop, dropDatabase and compact are refused inside a transaction. A follower refuses drop, dropDatabase and compact with read_only.
#listCollections
{"cmd":"listCollections"}{"collections":["users"],"ok":true}The names are sorted. A collection is listed if it holds a document or an index. An empty one with no indexes is not listed, because a restart would not bring it back. Needs read.
#drop
{"cmd":"drop","collection":"users"}{"dropped":true,"ok":true}Removes the collection and its indexes. dropped is false for a collection that is not listed (there was nothing to drop). Needs readWrite. Watchers get a drop event.
#listDatabases
{"cmd":"listDatabases"}{"databases":["shop"],"ok":true}A new server answers with an empty list. With users set up, only the databases the user has a grant on are listed.
#dropDatabase
{"cmd":"dropDatabase"}{"dropped":3,"ok":true}Closes the selected database (from use or db), deletes its log file, and replies with how many collections it had. Needs admin. The users stay, because they are not in a database. Watchers get a dropDatabase event.
dropDatabasecannot be undone. There is no confirmation.
#compact
{"cmd":"compact"}{"compacted":true,"ok":true}The log is append-only, so updates and deletes leave old entries behind. The server rewrites a log by itself once it is mostly out-of-date entries, so it does not grow forever. compact does that rewrite for the selected database now: it writes a new log with only what is live, syncs it, and swaps it in. Needs admin.
- The new log keeps the permissions of the one it replaces, and keeps every index with its options.
- A compaction that fails is not tried again until the log is a set number of entries longer.
- A follower never compacts. When a leader compacts, its followers start that database again from the whole new log, see Replication.
See Durability for what is written when.