Seed Identity Operation



Set a higher identity value

You can replace the latest identity value on the server with a new, higher number.

// Seed a higher identity value on the server:
// ===========================================

// Define the seed identity operation. Pass:
//   * The collection name (can be with or without a pipe)
//   * The new value to set
const seedIdentityOp = new SeedIdentityForOperation("companies|", 23);

// Execute the operation by passing it to maintenance.send
// The latest value on the server will be incremented to "23"
// and the next document created with an identity will be assigned "24"
const seededValue = await store.maintenance.send(seedIdentityOp);

// Create a document with an identity ID:
// ======================================

const company = new Company();
company.name = "RavenDB";

await session.store(company, "companies|");
await session.saveChanges();
// => Document "companies/24" will be created

Force a lower identity value

  • You can set the latest identity value to a number that is lower than the current latest value.

  • Before proceeding, first ensure that documents with an identity value higher than the new number do not exist.

// Force a smaller identity value on the server:
// =============================================

// Define the seed identity operation. Pass:
//   * The collection name (can be with or without a pipe)
//   * The new value to set
//   * Pass 'true' to force the update
const seedIdentityOp = new SeedIdentityForOperation("companies|", 5, true);

// Execute the operation by passing it to maintenance.send
// The latest value on the server will be decremented to "5"
// and the next document created with an identity will be assigned "6"
const seededValue = await store.maintenance.send(seedIdentityOp);

// Create a document with an identity ID:
// ======================================

const company = new Company();
company.name = "RavenDB";

await session.store(company, "companies|");
await session.saveChanges();
// => Document "companies/6" will be created

Syntax

const seedIdentityOp = new SeedIdentityForOperation(name, value, forceUpdate);
Parameter Type Description
name string The collection name to seed the identity value for.
Can be ended with or without a pipe (e.g. "companies" or "companies|".
value number The number to set as the latest identity value.
forceUpdate boolean true - force a new value that is lower than the latest.
false - only a higher value can be set.