Skip to content

Commit

Permalink
RDoc-2566 Delete by query - 5.2 Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielle9897 committed Jan 30, 2024
1 parent 781d038 commit a9380c5
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 /}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 /}
<br />

| 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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a9380c5

Please sign in to comment.