Update Entities


  • To modify existing documents:

    • Retrieve documents from the database using load or by a query.
      The entities loaded from the documents are added to the internal entities map that the Session manages.

    • Edit the properties you wish to change.
      The session will track all changes made to the loaded entities.

    • Save to apply the changes.
      Once saveChanges() returns it is guaranteed that the data is persisted in the database.

  • In this page:


Load a document & update

  • In this example we load a company document and update its postalCode property.

// Sample company document structure
class Address {
    constructor(code) {
        this.postalCode = code;
    }
}

class Company {
    constructor(name, code) {
        this.name = name;
        this.address = new Address(code);
    }
}

const session = documentStore.openSession();

// Load a company document
// The entity loaded from the document will be added to the Session's entities map
const company = await session.load("companies/1-A");

// Update the company's postalCode
company.address.postalCode = "TheNewPostalCode";

// Apply changes
await session.saveChanges();

Query for documents & update

  • In this example we query for company documents whose postalCode property is 12345,
    and modify this property for the matching documents.

const session = documentStore.openSession();

// Query: find companies with the specified postalCode
// The entities loaded from the matching documents will be added to the Session's entities map
const matchingCompanies = await session.query({collection: "Companies"})
    .whereEquals("address.postalCode", "12345")
    .all();

// Update the postalCode for the resulting company documents
matchingCompanies.forEach(c => c.address.postalCode = "TheNewPostalCode");

// Apply changes
await session.saveChanges();