SODA for In-Database JavaScriptによるコレクションからのドキュメントの削除
コレクションからのドキュメントの削除は置換と似ています。最初のステップは、検索操作(通常はドキュメントのキーに基づいて、またはSodaOperation.filter()の検索式を使用して)を実行することです。SodaOperation.remove()のコールはターミナル操作です。つまり、チェーンの最後の操作です。
例8-16 ドキュメント・キーによるコレクションからのドキュメントの削除
この例では、ドキュメント・キーが"100"のドキュメントを削除します。
export function removeByKey(searchKey){
// open MyCollection
const col = soda.openCollection("MyCollection");
if(col === null){
throw new Error("'MyCollection' does not exist");
}
// perform a lookup of the document about to be removed
// and ultimately remove it
const result = col
.find()
.key(searchKey)
.remove();
if(result.count === 0){
throw new Error(
`failed to delete a document with key ${searchKey}`
);
}
}
例8-17 フィルタによるコレクションからのJSONドキュメントの削除
この例では、フィルタを使用して、department_idが70のJSONドキュメントを削除します。その後、削除されたドキュメントの数を出力します。
export function removeByFilter(){
// open the collection
const col = soda.openCollection("MyCollection");
if(col === null){
throw new Error("'MyCollection' does not exist");
}
// perform a lookup based on a filter expression and remove
// the documents matching the filter
const result = col
.find()
.filter({"_id": 100})
.remove();
console.log(`${result.count} documents deleted`);
}