Single-Document Patching Examples: Documents and Metadata
-
This article is part of the single-document patching examples.
It focuses on loading related documents, patching document metadata, and adding, cloning, and deleting documents from a patch script. -
For the available patching interfaces (Session API, Session API using defer, and Operations API) and their full syntax,
see Patch a Single Document: API Overview. -
Patching examples in this article:
Patching examples
Loading documents in a script
- Loading related documents from a patch script is supported only by the defer or the operations syntax.
- The following example loads the product referenced by each order line and copies the product's current name to the order line.
- Session_defer_syntax
- Operations_syntax
// Update each order line with the current name from its referenced product document
session.Advanced.Defer(new PatchCommandData(
id: "orders/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = @"this.Lines.forEach(line => {
var product = load(line.Product);
if (product) {
line.ProductName = product.Name;
}
});"
},
patchIfMissing: null));
session.SaveChanges();
// Update each order line with the current name from its referenced product document
store.Operations.Send(new PatchOperation(
id: "orders/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = @"this.Lines.forEach(line => {
var product = load(line.Product);
if (product) {
line.ProductName = product.Name;
}
});"
},
patchIfMissing: null));
Patch document metadata
- A patch script can modify a document's metadata by accessing
this['@metadata']. - The following example sets
@expiresto one minute in the future.
When document expiration is enabled, RavenDB deletes the document after this time is reached. - Patching metadata with a script is supported only by the defer or operations syntax.
- Session_defer_syntax
- Operations_syntax
// Schedule the document for expiration
session.Advanced.Defer(new PatchCommandData(
id: "users/1",
changeVector: null,
patch: new PatchRequest
{
Script = "this['@metadata']['@expires'] = args.expires;",
Values =
{
// @expires must contain an ISO 8601 UTC date-time string
{ "expires", DateTime.UtcNow.AddMinutes(1).ToString("O") }
}
},
patchIfMissing: null));
session.SaveChanges();
// Schedule the document for expiration
store.Operations.Send(new PatchOperation(
id: "users/1",
changeVector: null,
patch: new PatchRequest
{
Script = "this['@metadata']['@expires'] = args.expires;",
Values =
{
// @expires must contain an ISO 8601 UTC date-time string
{ "expires", DateTime.UtcNow.AddMinutes(1).ToString("O") }
}
},
patchIfMissing: null));
Add document
- Use the
put(documentId, document)patch-script function to store another document. - In this example:
- The
projects/prefix tells RavenDB to generate a new project ID. id(this)returns the ID of the employee being patched and stores it inProjectLeader.@metadata.@collectionplaces the new document in theProjectscollection.
- The
- Adding a document from a patch script is supported only by the defer or the operations syntax.
- Session_defer_syntax
- Operations_syntax
// Create a project with a server-generated ID
// and reference the patched employee as its project leader
session.Advanced.Defer(new PatchCommandData(
id: "employees/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = @"put('projects/', {
ProjectLeader: id(this),
ProjectDesc: 'New project',
'@metadata': { '@collection': 'Projects' }
});"
},
patchIfMissing: null));
session.SaveChanges();
// Create a project with a server-generated ID
// and reference the patched employee as its project leader
store.Operations.Send(new PatchOperation(
id: "employees/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = @"put('projects/', {
ProjectLeader: id(this),
ProjectDesc: 'New project',
'@metadata': { '@collection': 'Projects' }
});"
},
patchIfMissing: null));
Clone document
- To clone a document from a patch script, pass the current document (
this) toputwith a new document ID.
Theemployees/prefix tells RavenDB to generate the clone's ID. - Cloning a document from a patch script is supported only by the defer or the operations syntax.
- Session_defer_syntax
- Operations_syntax
// Clone the current document under a new server-generated employee ID
session.Advanced.Defer(new PatchCommandData(
id: "employees/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = "put('employees/', this);"
},
patchIfMissing: null));
session.SaveChanges();
// Clone the current document under a new server-generated employee ID
store.Operations.Send(new PatchOperation(
id: "employees/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = "put('employees/', this);"
},
patchIfMissing: null));
Document data, related items, and revisions:
- The clone receives a copy of the source document's body.
- Attachments, counters, and time series are stored separately and are not copied automatically.
The source document's revision history is not copied either. - To manage related items from a patch script, use the predefined JavaScript methods:
- When cloning a document via Studio,
Studio copies the document body, attachments, counters, and time series.
It does not copy the source document's revision history.
Archived documents:
- If the source document is archived, the cloned document will not be archived.
See Data archival overview.
Delete document
- Use RavenDB's
del(documentId)patch-script function to delete the document with the specified ID. - The example below passes
id(this)to delete the document currently being patched. - Do not confuse
del(...)with JavaScript'sdeleteoperator.
For example,delete this.Extensionremoves only theExtensionproperty from the document.
See Remove property. - Deleting a document from a patch script is supported only by the defer or the operations syntax.
- Session_defer_syntax
- Operations_syntax
// Delete the document currently being patched
session.Advanced.Defer(new PatchCommandData(
id: "employees/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = "del(id(this));"
},
patchIfMissing: null));
session.SaveChanges();
// Delete the document currently being patched
store.Operations.Send(new PatchOperation(
id: "employees/1-A",
changeVector: null,
patch: new PatchRequest
{
Script = "del(id(this));"
},
patchIfMissing: null));