Collection Operations
- Deprecated:
store.collection("name")
- DQL: Use collection name directly in queries
Creating Documents
In DQL syntax, enclose
string
literals within single quotes (''
); for example 'blue'
.Use single quotes in DQL queries to ensure accuracy. Incorrectly using double quotes (
""
); for example "blue"
, may prevent document matching. Currently, Ditto does not throw a warning for this issue.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:
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.
PN_INCREMENT
keyword followed by the
value you want to increment the value by.
To decrement a counter, use a negative value.
Attachments
- Store methods (NOT deprecated):
store.new_attachment(path, metadata)
- Still availablestore.fetch_attachment(token, handler)
- Still available
- Collection methods (deprecated):
collection.new_attachment()
→ Usestore.new_attachment()
collection.fetch_attachment()
→ Usestore.fetch_attachment()
Registers
AREGISTER
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