Skip to main content

Single-Document Patching Examples: Documents and Metadata

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.
// 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();

Patch document metadata

  • A patch script can modify a document's metadata by accessing this['@metadata'].
  • The following example sets @expires to 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.
// 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();

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 in ProjectLeader.
    • @metadata.@collection places the new document in the Projects collection.
  • Adding a document from a patch script is supported only by the defer or the 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();

Clone document

  • To clone a document from a patch script, pass the current document (this) to put with a new document ID.
    The employees/ 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.
// 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();

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:

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's delete operator.
    For example, delete this.Extension removes only the Extension property from the document.
    See Remove property.
  • Deleting a document from a patch script is supported only by the defer or the 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();

In this article