MongoDB Document Types

MongoDB uses BSON documents for three things:

  1. Data storage (user documents). These are the regular JSON-like objects that the database stores for us. These BSON documents are sent to the database via the INSERT operation. User documents have limitations on the “element name” space due to the usage of special characters in the JSON-like query language.
    • A user document element name cannot begin with $.
    • A user document element name cannot have a . in the name.
    • The element name _id is reserved for use as a primary key id, but you can store anything that is unique in that field. The database expects that drivers will prevent users from creating documents that violate these constraints.
  2. Query “Selector” Documents: Query documents (or selectors) are BSON documents that are used in QUERY, DELETE and UPDATE operations. They are used by these operations to match against documents. Selector objects have no limitations on the “element name” space, as they must be able to supply special “marker” elements, like $where and the special “command” operations.
  3. “Modifier” Documents : Documents that contain ‘modifier actions’ that modify user documents in the case of an update.