v0.7.0
Menu

#Requests and replies

A connection carries one request per line and one reply per line, both JSON. The server answers requests in order. A request is a JSON document with a cmd field and the fields that command takes.

{"cmd":"find","collection":"users","filter":{"age":{"$gt":18}}}

A connection can switch to a binary encoding of the same requests, see Wire format. Everything on this page is about the JSON form.

The first line of a connection has to start with {, or be the four bytes that start the binary format. Anything else closes the connection. That stops a web page from making a browser send HTTP to the port and have its body run as commands.

#Replies

A successful reply has "ok":true and the fields of the command. Reply fields come in alphabetical order, so do not rely on their order.

{"count":2,"ok":true}

A failed request gets "ok":false, a code and an error message:

{"code":"invalid","error":"unknown operator $bad","ok":false}

The code says whose mistake it is, so a client does not need to read the message. The messages are English text and are not meant to be parsed. See Errors.

#Commands

Command Reference
insert, get, find, count, distinct Filters, Updates and writes
update, replace, delete, findOneAndUpdate, findOneAndDelete Updates and writes
aggregate Aggregation
createIndex, dropIndex, listIndexes Indexes
begin, commit, abort Transactions
watch Change streams
use, listDatabases, listCollections, drop, dropDatabase, compact Collections and databases
auth, scramStart, scramFinish, createUser, updateUser, dropUser, users Authentication
replicationStatus, replicate Replication

An unknown cmd gets bad_request. Most commands take a collection, and a missing one is also bad_request.

#Databases

Every request is for one database. Name it with a db field in the request, or once with use for the whole connection:

{"cmd":"use","db":"shop"}

A db in a request overrides the one from use:

{"cmd":"find","collection":"users","db":"logs"}

With neither, the request is a bad_request: no database, send a db with the request or use one first.

  • A database name is 1 to 64 letters, digits, _ or -. The names admin, local and _users are not allowed.
  • A database is made by the first write to it. Reading one that does not exist finds it empty, so find gives no documents, and use on a database that does not exist yet succeeds.
  • Collections are made on first use in the same way.
  • Users are not in a database. They belong to the whole server.

Each database is one log file in the data directory, named <name>.log.

#Values

A document holds these types:

Type In JSON
null null
bool true, false
int a whole number, as a 64 bit signed integer
float a number with a . or an exponent
string a string
array an array
document an object
ObjectId {"$oid":"<24 hex digits>"}
date {"$date":<milliseconds since 1970 UTC>}
regex {"$regularExpression":{"pattern":"...","options":"..."}}

#Ids

Every document has an _id. If you insert a document without one, the server makes an ObjectId, and the reply to the insert has it:

{"id":{"$oid":"6b12e5a20000c4e849f2795a"},"ok":true}

An _id you give has to be an ObjectId, and it cannot be changed afterwards. Ids start with the time they were made, so documents with equal sort keys come out in roughly the order they were made. Inserting an _id that is taken is a duplicate error.

Commands that take one document by id (get, delete) take it in this form:

{"cmd":"get","collection":"users","id":{"$oid":"000000000000000000000001"}}

#Dates

A date is {"$date":1700000000000}. Only a whole number of milliseconds is read as a date, so {"$date":"2020-01-01"} stays an ordinary document, and the server does not parse dates from text. A date compares only with dates, and a number is not a date. Dates can be indexed, and found by equality, $in and sort. $currentDate sets one.

#Numbers

1 is an int and 1.0 is a float. They compare as equal numerically and share a group in $group and a key in an index, but they stay different types when stored, and a float is written back with a .. Arguments such as limit and skip have to be whole numbers. A number that would be infinite (1e400, or an $inc that overflows) is refused, and so is NaN, which JSON cannot write.

#Field names

A field name cannot be empty, contain a ., or start with $, at any depth. A dot would be ambiguous with a path, and a $ would be read as an operator.

#Limits

A request line is at most 16 MiB, holds at most a million values, and nests at most 100 levels. A document can be at most 99 levels deep. A reply is at most 64 MiB, and a find that is already over that stops and says so, so use limit to get fewer documents. See Memory and limits for the connection limits and the time limits.