v0.7.0
Menu

#Updates and writes

#insert

One document, or a list with docs. A list is one request, one log write, and all or nothing.

{"cmd":"insert","collection":"users","doc":{"name":"Dylan","age":25}}
{"cmd":"insert","collection":"users","docs":[{"name":"Ann"},{"name":"Bob"}]}

The reply has id for doc and ids for docs. doc and docs together are a bad_request. An _id that is taken is a duplicate error, and a unique index that would be violated is duplicate_key.

#get

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

The reply is {"doc":{...},"ok":true}, or "doc":null when there is none. It takes projection, as find does.

#update

{"cmd":"update","collection":"users","filter":{"name":"Dylan"},"update":{"$inc":{"age":1}},"limit":1}

The reply is {"count":1,"ok":true}, where count is the number of documents the update was applied to. There is no separate matched and modified count. Without limit it changes every match, and limit has to be at least 1. Which documents a limit picks is not defined, see Filters.

The update is checked once before any document is touched, so a bad update is an error even when nothing matches.

#Update operators

Operator Does
$set sets a field, or a position of an array
$unset removes a field. Any value, such as "". A position in an array becomes null and the others do not shift. _id cannot be unset
$inc adds a number. Integer overflow and a result that is not finite are errors. A missing field is set to the amount
$mul multiplies. A missing field becomes 0 of the factor's kind (0 or 0.0)
$min, $max sets the field if there is none, or if the value comes first (last) in sort order. Equal keeps what was there, so 5 stays an int under $max: 5.0
$rename {"old":"new"} moves the value and overwrites what new held. A missing old is left alone. Neither can be _id, or a position in an array, or go through an array
$currentDate {path:true} or {path:{"$type":"date"}} sets the time. One time is used for every document the update changes
$setOnInsert like $set, but only when an upsert makes a document
$push adds to the end of an array, making the array if the field is missing. A field that is not an array is an error
$addToSet adds values that are not there already (2.0 counts as 2). Takes $each
$pop {path:1} removes the last element, {path:-1} the first. An empty array or missing field is left alone
$pull removes every element that meets a condition. A plain value removes equal elements, a document of operators ({"$gte":9}) removes those that meet them, and any other document is a filter for elements that are documents
$pullAll {path:[values]} removes the elements equal to any listed value
$bit {path:{"or":n}} with one of and, or or xor, on whole numbers. A missing field counts as 0
{"cmd":"update","collection":"orders","filter":{"cust":"a"},"update":{"$set":{"status":"paid"},"$inc":{"qty":1},"$unset":{"draft":""}}}

$push takes modifiers inside a document with $each, applied in this order:

Modifier Meaning
$each the values to add
$position where to insert. Negative counts from the end. Stops at the ends
$sort sorts the whole array afterwards: 1, -1, or {"field":1} with one field only
$slice keeps the first n, or the last n when negative
{"cmd":"update","collection":"orders","filter":{"cust":"a"},"update":{"$push":{"tags":{"$each":["q","r"],"$position":0,"$slice":5}}},"limit":1}

#Rules for paths

  • A path creates the documents it goes through. $set on "scores.2" sets that position of an array, or adds to its end if it is the next one. Going further than that is an error. A field name, or a position inside something that is not an array or document, is an error. An array is never made by a path: a.0 on a missing a makes a document with a field called 0.
  • Two targets cannot overlap: a and a.b, or a under both $set and $inc. The result would depend on which went first, so the update is refused with invalid. This includes $addToSet and $pull on the same array in one update.
  • _id cannot be changed, and neither can anything below it.
  • Field names in values cannot be empty, contain a . or start with $.
  • Documents cannot be nested more than 99 levels.
  • A name that starts with $ in a pushed value is refused.

#Positional operators

A path in an update can contain a part that stands for elements of an array.

Path Updates
items.$[].price every element
items.$[name].price the elements that the array filter for name lets through
items.$.price the first element that the filter of the update matched

For $[name], give arrayFilters: a document for each name, with conditions on name (the element) or name.field. All the conditions of one filter have to hold, and a filter can use $and and $or. Names are a lower case letter, then letters and digits.

{"cmd":"update","collection":"orders","filter":{"cust":"a"},"update":{"$set":{"items.$[p].price":0}},"arrayFilters":[{"p.price":{"$gte":5}}]}
{"cmd":"update","collection":"orders","filter":{"items.k":"x"},"update":{"$inc":{"items.$.price":1}}}
  • The array has to be there. a.$[] on a missing field or a number is an error. An empty array changes nothing.
  • A name with no filter, a filter with no name, two filters for one name, or one filter about two names is an error.
  • The filter for $ has to name the array directly or inside an $and. No match, or a filter with $or in it, is an error.
  • $ has to be alone in its path and cannot come first. $rename cannot use any of them.
  • A position and a positional part count as the same target, so a.$[].x with a.0.y is refused, and so are a few that do not really clash.

#Upserts

"upsert":true on update inserts a document when nothing matches. It starts from the plain equalities of the filter (at the top and inside $and, with a path like a.b made into a nested document), then applies the update to it, with $setOnInsert as a $set.

{"cmd":"update","collection":"users","filter":{"name":"Zed"},"update":{"$set":{"age":5},"$setOnInsert":{"x":1}},"upsert":true}
{"count":1,"ok":true,"upserted":{"$oid":"6b12e5a20000c4e849f2795d"}}

When a document is made the reply has upserted, its id. When one was found it is the usual count. The _id is taken from an equality on _id in the filter, or made.

  • Ranges, $in, $or, $not and the like say nothing to copy and are left out.
  • Two conditions that cannot both be true of one document (a:1 with a:2, or a:1 with a.b:2) are an error.
  • A positional $ with nothing to match, and $[name] on an array that is not there, are errors on an upsert.
  • The find and the insert happen under one write lock, so no other request comes between. With limit, one document is made.

#replace

Replaces the first match with doc, keeping its _id.

{"cmd":"replace","collection":"users","filter":{"name":"Zed"},"doc":{"name":"Zed","age":6}}

Optional fields are sort (to pick which match) and upsert. A doc with a different _id is an error, and so are field names that start with $. The reply is as for update: count, and upserted when an upsert made a document. An upserting replace takes _id from doc, else from an equality on _id in the filter, else makes one.

#findOneAndUpdate and findOneAndDelete

These pick one document, change it and return it, under one lock, so nothing happens in between.

{"cmd":"findOneAndUpdate","collection":"users","filter":{"name":"Ann"},"update":{"$inc":{"age":1}},"return":"after"}
{"cmd":"findOneAndDelete","collection":"users","filter":{"name":"Bob"}}
Field Meaning
filter, update as for update
sort which match to take when there are several
upsert as for update
arrayFilters as for update
projection which fields come back
return "before" (the default) or "after"

The reply is {"doc":{...},"ok":true}, or "doc":null if nothing matched. With upsert and no match, "return":"before" gives null. findOneAndDelete takes filter, sort and projection.

#delete

By id, or by filter, not both:

{"cmd":"delete","collection":"users","id":{"$oid":"000000000000000000000001"}}
{"cmd":"delete","collection":"users","filter":{"age":{"$lt":18}},"limit":5}

The reply is {"deleted":1,"ok":true}, a count that is 0 or 1 for an id. Without limit a filter deletes every match, and an empty filter {} deletes everything in the collection, on purpose, as in MongoDB's deleteMany({}). A collection that does not exist is not made by update or delete.

#distinct

{"cmd":"distinct","collection":"users","field":"address.city"}
{"ok":true,"values":["Leeds","York"]}

Each value once, in sort order, with the elements of an array counted one by one. It takes an optional filter. 1 and 1.0 count as one value, and whichever came first is given.

#count

{"cmd":"count","collection":"users","filter":{"age":{"$gte":18}}}

See Filters. With no filter it is the size of the collection.

#Permissions

With users set up, find, get, count, aggregate (without $out or $merge), distinct and listCollections need the read role. Writes need readWrite, and dropDatabase and compact need admin. See Authentication.