From a9380c526c5a56f510ddf28830e97b99a8aa7fc4 Mon Sep 17 00:00:00 2001 From: danielle9897 Date: Tue, 30 Jan 2024 17:25:20 +0200 Subject: [PATCH] RDoc-2566 Delete by query - 5.2 Node.js --- .../common/delete-by-query.dotnet.markdown | 2 +- .../common/delete-by-query.js.markdown | 130 +++++++++++++++++ .../Operations/Common/DeleteByQuery.cs | 10 +- .../Operations/Common/deleteByQuery.js | 131 ++++++++++++++++++ 4 files changed, 267 insertions(+), 6 deletions(-) create mode 100644 Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.js.markdown create mode 100644 Documentation/5.2/Samples/nodejs/ClientApi/Operations/Common/deleteByQuery.js diff --git a/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.dotnet.markdown b/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.dotnet.markdown index 4f6b30bedc..ea47838a91 100644 --- a/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.dotnet.markdown +++ b/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.dotnet.markdown @@ -64,7 +64,7 @@ from "Orders" where Freight > 30 {NOTE: } -__A sample index__: +__A sample Map-index__: {CODE the_index@ClientApi\Operations\Common\DeleteByQuery.cs /} diff --git a/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.js.markdown b/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.js.markdown new file mode 100644 index 0000000000..0d8e6db731 --- /dev/null +++ b/Documentation/5.2/Raven.Documentation.Pages/client-api/operations/common/delete-by-query.js.markdown @@ -0,0 +1,130 @@ +# Delete by Query Operation +--- + +{NOTE: } + +* Use `DeleteByQueryOperation` to delete a large number of documents that match the provided query in a single server call. + +* __Dynamic behavior__: + The deletion of documents matching the specified query is run in batches of size 1024. + During the deletion process, documents that are added/modified __after__ the delete operation has started + may also be deleted if they match the query criteria. + +* __Background operation__: + This operation is performed in the background on the server. + If needed, you can __wait__ for the operation to complete. See: [Wait for completion](../../../client-api/operations/what-are-operations#wait-for-completion). + +* In this page: + * [Delete by dynamic query](../../../client-api/operations/common/delete-by-query#delete-by-dynamic-query) + * [Delete by index query](../../../client-api/operations/common/delete-by-query#delete-by-index-query) + * [Syntax](../../../client-api/operations/common/delete-by-query#syntax) + +{NOTE/} + +{PANEL: Delete by dynamic query} + +{NOTE: } + +__Delete all documents in collection__: + +{CODE-TABS} +{CODE-TAB:nodejs:DeleteOperation delete_by_query_0@ClientApi\Operations\Common\deleteByQuery.js /} +{CODE-TAB-BLOCK:sql:RQL} +from "Orders" +{CODE-TAB-BLOCK/} +{CODE-TABS/} + +{NOTE/} + +{NOTE: } + +__Delete with filtering__: + +{CODE-TABS} +{CODE-TAB:nodejs:DeleteOperation delete_by_query_1@ClientApi\Operations\Common\deleteByQuery.js /} +{CODE-TAB-BLOCK:sql:RQL} +from "Orders" where Freight > 30 +{CODE-TAB-BLOCK/} +{CODE-TABS/} + +{NOTE/} + +{PANEL/} + +{PANEL: Delete by index query} + +* `DeleteByQueryOperation` can only be performed on a __Map-index__. + An exception is thrown when executing the operation on a Map-Reduce index. + +* A few overloads are available, see the following examples: + +--- + +{NOTE: } + +__A sample Map-index__: + +{CODE:nodejs the_index@ClientApi\Operations\Common\deleteByQuery.js /} + +{NOTE/} + +{NOTE: } + +__Delete documents via an index query__: + +{CODE-TABS} +{CODE-TAB:nodejs:DeleteOperation delete_by_query_2@ClientApi\Operations\Common\deleteByQuery.js /} +{CODE-TAB:nodejs:DeleteOperation_overload delete_by_query_3@ClientApi\Operations\Common\deleteByQuery.js /} +{CODE-TAB-BLOCK:sql:RQL} +from index "Products/ByPrice" where Price > 10 +{CODE-TAB-BLOCK/} +{CODE-TABS/} + +{NOTE/} + +{NOTE: } + +__Delete with options__: + +{CODE-TABS} +{CODE-TAB:nodejs:DeleteOperation delete_by_query_4@ClientApi\Operations\Common\deleteByQuery.js /} +{CODE-TAB-BLOCK:sql:RQL} +from index "Products/ByPrice" where Price > 10 +{CODE-TAB-BLOCK/} +{CODE-TABS/} + +* Specifying `options` is also supported by the other overload methods, see the Syntax section below. + +{NOTE/} + +{PANEL/} + +{PANEL: Syntax} + +{CODE:nodejs syntax_1@ClientApi\Operations\Common\deleteByQuery.js /} +
+ +| Parameter | Type | Description | +|-------------------|-----------------------------|------------------------------------------------------------| +| __queryToDelete__ | string | The RQL query to perform | +| __queryToDelete__ | `IndexQuery` | Holds all the information required to query an index | +| __options__ | `object` | Object holding different setting options for the operation | + +{CODE:nodejs syntax_2@ClientApi\Operations\Common\DeleteByQuery.js /} + +{PANEL/} + +## Related Articles + +### Operations + +- [What are Operations](../../../client-api/operations/what-are-operations) + +### Client API + +- [How to Query](../../../client-api/session/querying/how-to-query) + +### Querying + +- [What is RQL](../../../client-api/session/querying/what-is-rql) +- [Querying an index](../../../indexes/querying/query-index) diff --git a/Documentation/5.2/Samples/csharp/Raven.Documentation.Samples/ClientApi/Operations/Common/DeleteByQuery.cs b/Documentation/5.2/Samples/csharp/Raven.Documentation.Samples/ClientApi/Operations/Common/DeleteByQuery.cs index 99d47d0d54..619cef2697 100644 --- a/Documentation/5.2/Samples/csharp/Raven.Documentation.Samples/ClientApi/Operations/Common/DeleteByQuery.cs +++ b/Documentation/5.2/Samples/csharp/Raven.Documentation.Samples/ClientApi/Operations/Common/DeleteByQuery.cs @@ -60,7 +60,7 @@ public DeleteByQuery() // * All documents matching the specified RQL will be deleted from the server. - // * Since the dynamic query was made with a filtering condition - + // * Since the dynamic query was made with a filtering condition, // an auto-index is generated (if no other matching auto-index already exists). #endregion } @@ -139,7 +139,7 @@ public DeleteByQuery() Query = "from index 'Products/ByPrice' where Price > 10" }, // OPTIONS: Specify the options for the operation - // (See all other available options under the Syntax section) + // (See all other available options in the Syntax section below) new QueryOperationOptions { // Allow the operation to operate even if index is stale @@ -189,7 +189,7 @@ public async Task DeleteByQueryAsync() // * All documents matching the provided RQL will be deleted from the server. - // * Since a dynamic query was made with a filtering condition - + // * Since a dynamic query was made with a filtering condition, // an auto-index is generated (if no other matching auto-index already exists). #endregion } @@ -205,7 +205,7 @@ public async Task DeleteByQueryAsync() Query = "from index 'Products/ByPrice' where Price > 10" }, // OPTIONS: Specify the options for the operation - // (See all other available options under the Syntax section) + // (See all other available options in the Syntax section below) new QueryOperationOptions { // Allow the operation to operate even if index is stale @@ -267,7 +267,7 @@ public class QueryOperationOptions // If timeout is exceeded then exception is thrown. public TimeSpan? StaleTimeout { get; set; } - // Limits the amount of base operation per second allowed. + // Limits the number of base operations per second allowed. public int? MaxOpsPerSecond // Determines whether operation details about each document should be returned by server. diff --git a/Documentation/5.2/Samples/nodejs/ClientApi/Operations/Common/deleteByQuery.js b/Documentation/5.2/Samples/nodejs/ClientApi/Operations/Common/deleteByQuery.js new file mode 100644 index 0000000000..1685c36db2 --- /dev/null +++ b/Documentation/5.2/Samples/nodejs/ClientApi/Operations/Common/deleteByQuery.js @@ -0,0 +1,131 @@ +import { DocumentStore, AbstractIndexCreationTask } from "ravendb"; + +const store = new DocumentStore(); +const session = store.openSession(); + +{ + //region the_index + // The index definition: + // ===================== + + class Products_ByPrice extends AbstractJavaScriptIndexCreationTask { + constructor () { + super(); + + this.map("products", product => { + return { + Price: product.PricePerUnit + }; + }); + } + } + //endregion + + async function deleteByQuery() { + { + //region delete_by_query_0 + // Define the delete by query operation, pass an RQL querying a collection + const deleteByQueryOp = new DeleteByQueryOperation("from 'Orders'"); + + // Execute the operation by passing it to operations.send + const operation = await store.operations.send(deleteByQueryOp); + + // All documents in collection 'Orders' will be deleted from the server. + //endregion + } + + { + //region delete_by_query_1 + // Define the delete by query operation, pass an RQL querying a collection + const deleteByQueryOp = new DeleteByQueryOperation("from 'Orders' where Freight > 30"); + + // Execute the operation by passing it to operations.send + const operation = await store.operations.send(deleteByQueryOp); + + // * All documents matching the specified RQL will be deleted from the server. + + // * Since the dynamic query was made with a filtering condition, + // an auto-index is generated (if no other matching auto-index already exists). + //endregion + } + + { + //region delete_by_query_2 + // Define the delete by query operation, pass an RQL querying the index + const deleteByQueryOp = + new DeleteByQueryOperation("from index 'Products/ByPrice' where Price > 10"); + + // Execute the operation by passing it to operations.send + const operation = await store.operations.send(deleteByQueryOp); + + // All documents with document-field PricePerUnit > 10 will be deleted from the server. + //endregion + } + + { + //region delete_by_query_3 + // Define the index query, provide an RQL querying the index + const indexQuery = new IndexQuery(); + indexQuery.query = "from index 'Products/ByPrice' where Price > 10"; + + // Define the delete by query operation + const deleteByQueryOp = new DeleteByQueryOperation(indexQuery); + + // Execute the operation by passing it to operations.send + const operation = await store.operations.send(deleteByQueryOp); + + // All documents with document-field PricePerUnit > 10 will be deleted from the server. + //endregion + } + + { + //region delete_by_query_4 + // QUERY: Define the index query, provide an RQL querying the index + const indexQuery = new IndexQuery(); + indexQuery.query = "from index 'Products/ByPrice' where Price > 10"; + + // OPTIONS: Define the operations options + // (See all available options in the Syntax section below) + const options = { + // Allow the operation to operate even if index is stale + allowStale: true, + // Limit the number of base operations per second allowed. + maxOpsPerSecond: 500 + } + + // Define the delete by query operation + const deleteByQueryOp = new DeleteByQueryOperation(indexQuery, options); + + // Execute the operation by passing it to operations.send + const operation = await store.operations.send(deleteByQueryOp); + + // All documents with document-field PricePerUnit > 10 will be deleted from the server. + //endregion + } + } +} + +{ + //region syntax_1 + // Available overload: + // =================== + const deleteByQueryOp = new DeleteByQueryOperation(indexQuery); + const deleteByQueryOp = new DeleteByQueryOperation(indexQuery, options); + //endregion + + //region syntax_2 + // options object + { + // Indicates whether operations are allowed on stale indexes. + allowStale, // boolean + + // If AllowStale is set to false and index is stale, + // then this is the maximum timeout to wait for index to become non-stale. + // If timeout is exceeded then exception is thrown. + staleTimeout, // number + + // Limits the number of base operations per second allowed. + maxOpsPerSecond, // number + } + //endregion +}