Session: How to Get and Modify Entity Metadata

When a document is downloaded from the server, it contains various metadata information like ID or current change-vector. This information is stored in a session and is available for each entity using the getMetadataFor method from the advanced session operations.

Get the Metadata

session.advanced.getMetadataFor(entity);
Parameters
entity object Entity for which metadata will be returned.
Return Value
object Returns the metadata for the specified entity. Throws an exception if the instance is not tracked by the session.

Example

const employee = await session.load("employees/1-A");
const metadata = session.advanced.getMetadataFor(employee);

Modify the Metadata

After getting the metadata from session.advanced.getMetadataFor() you can modify it.

Note

Keys in the metadata that starting with @ are reserved for RavenDB use

Example I

const user = new User();
user.name = "Idan";

await session.store(user);

const metadata = session.advanced.getMetadataFor(user);
metadata["Permissions"] = "READ_ONLY";
await session.saveChanges();

Example II

const user = await session.load("users/1-A");
const metadata = session.advanced.getMetadataFor(user);
metadata["Permissions"] = "READ_AND_WRITE";
await session.saveChanges();