Skip to main content

Collection Operations

  • Deprecated: store.collection("name")
  • DQL: Use collection name directly in queries

Creating Documents

In DQL syntax, enclose string literals within single quotes ('') or from version 5.0, double quotes(""); for example 'blue' or "blue".
Prior to version 5.0 only use single quotes in DQL queries. Incorrectly using double quotes (""), for example field = "blue", may raise an error or produce unexpected results (see: Quoted Identifiers).

Inserting a Single Document

  • Deprecated: collection.insert({...})
  • DQL: store.execute("INSERT INTO COLLECTION name DOCUMENTS (:doc)", {{"doc", json_doc}})

Inserting Multiple Documents

Inserting Initial/Default Documents

This is the ability to insert documents at time 0. For more information see INSERT with INITIAL DOCUMENTS.

Upserting Documents

  • Deprecated: collection.upsert({...})
  • DQL: store.execute("INSERT INTO COLLECTION name DOCUMENTS (:doc) ON ID CONFLICT DO UPDATE", {{"doc", json_doc}})
  • Note: Use ON ID CONFLICT DO UPDATE to get true upsert behavior (insert or update)
  • Note: If just inserting new documents without an _id, omit the ON ID CONFLICT clause

Reading Data

Querying Data

  • Deprecated: collection.find("field == $args.value").with_args({{"value", x}}).exec()
  • DQL: store.execute("SELECT * FROM COLLECTION name WHERE field = :value", {{"value", x}})

Querying Data by ID

  • Deprecated: collection.find_by_id(doc_id).exec()
  • DQL (4.11+): store.execute("SELECT * FROM COLLECTION name USE IDS :id", {{"id", doc_id}})
  • DQL (all versions): store.execute("SELECT * FROM COLLECTION name WHERE _id = :id", {{"id", doc_id}})

Querying Data with Limits

Querying for Null Values

Querying with Arguments

Observing Data Changes

Updating Documents

Updating a Single Document

  • Deprecated: collection.find_by_id(id).update({ ... })
  • DQL: store.execute("UPDATE COLLECTION name SET field = :value WHERE _id = :id", params)

Updating Multiple Documents

Updating Multiple Fields

Updating Nested Fields

Counters

Available in 4.11 and later with DQL_STRICT_MODE=false.
Use the APPLY keyword followed by the field name and then the PN_INCREMENT keyword followed by the value you want to increment the value by. To decrement a counter, use a negative value.
For more information on using Counters see here.

Attachments

  • Store methods (NOT deprecated):
    • store.new_attachment(path, metadata) - Still available
    • store.fetch_attachment(token, handler) - Still available
  • Collection methods (deprecated):
    • collection.new_attachment() → Use store.new_attachment()
    • collection.fetch_attachment() → Use store.fetch_attachment()

Registers

A REGISTER is a data type in Ditto that stores a single scalar value and uses last-write-wins merge strategy for handling conflicts Key characteristics of REGISTER:
  • Stores primitive types (string, boolean) or JSON objects
  • Last-write-wins conflict resolution ensures consistent values across peers
  • With strict mode disabled, if you want a REGISTER Map data type in DQL, it must be specified explicitly
For more information on using types and definitions, see Registers.

Deletion

Deleting Fields

Evicting Documents

Deleting Documents

There are several ways to delete data in Ditto, and you can find more detail about them here.

Syncing Data From Other Peers