Replacing Documents in a Collection with SODA for In-Database JavaScript
To replace the content of one document in a collection with the content of
another, you start by looking up the document to be modified using its key. Because
SodaOperation.key()
is a nonterminal operation, the easiest way to replace
the contents is to chain SodaOperation.key()
to
SodaOperation.replaceOne()
or
SodaOperation.replaceOneAndGet()
.
SodaOperation.replaceOne()
merely replaces the document, whereas
SodaOperation.replaceOneAndGet()
replaces it and provides the resulting
new document to the caller.
The difference between SodaOperation.replace()
and
SodaOperation.save()
is that the latter performs an insert in case the
key doesn't already exist in the collection. The replace operation requires an existing
document to be found by the lookup via the SodaOperation.key()
method.
Note:
Some version-generation methods generate hash values of the document content. In such a case, if the document content does not change then neither does the version.
Example 8-15 Replacing a Document in a Collection and Returning the Result Document
This example shows how to replace a document in a collection, returning a reference to the changed document. Let's assume that employee 206 has been given a raise of 100 monetary units. Using the SODA API you can update the salary as follows:
export function replaceExample(){
// open employeesCollection in preparation of the update
const col = soda.openCollection('employeesCollection');
if (col === null){
throw new Error("'employeesCollection does not exist");
}
try{
// look up employeeId 206 using a QBE and get the document.
// Since the documents are inserted into the collection based
// on the HR.employees table, it is certain that there is at
// most 1 document with employeeId 206
const employeeDoc = col
.find()
.filter({"_id": 206})
.getOne();
// get the document's actual contents/payload
employee = employeeDoc.getContent();
// currently it is not possible to include the _id together with
// the replacement payload. This means existing _id must be deleted.
// The document, once replaced in the collection, will have its
// _id injected from the target document
delete employee_id;
// increase the salary
employee.salary += 100;
// save the document back to the collection. Note that you need
// to provide the document's key rather than a QBE or else an
// ORA-40734: key for the document to replace must be specified
// using the key attribute error will be thrown
const resultDoc = col
.find()
.key(employeeDoc.key)
.replaceOneAndGet(employee);
// print some metadata (note that content is not returned for
// performance reasons)
console.log(`Document updated successfully:
- key: ${resultDoc.key}
- version: ${resultDoc.version}`);
} catch(err){
console.log(`error modifying employee 206's salary: ${err}`);
}
}
See Example 8-8 for details about how to create employeesCollection
, used
in this example.
Note:
Trying to read the changed contents will result in an error as the actual document's contents aren't returned, for performance reasons.Parent topic: Using SODA for In-Database JavaScript