Find and delete a document
Finds a single document in a collection using filter and sort clauses, and then deletes that document.
This method and the method to delete a document have the same database effect but differ in their return value. This method returns the document that was found and deleted. The method to delete a document returns details about the success of the deletion.
Method signature
-
Python
-
TypeScript
-
Java
-
curl
collection.find_one_and_delete(
filter: Dict[str, Any],
*,
projection: Iterable[str] | Dict[str, bool],
sort: Dict[str, Any],
max_time_ms: int,
) -> Dict[str, Any]
collection.findOneAndDelete(
filter: Filter<Schema>,
options?: {
projection?: Projection,
sort?: Sort,
includeResultMetadata?: boolean,
maxTimeMS?: number,
},
): Promise<ModifyResult<Schema>>
Optional<T> findOneAndDelete(
Filter filter
)
Optional<T> findOneAndDelete(
Filter filter,
FindOneAndDeleteOptions options
)
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/COLLECTION_NAME" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": FILTER,
"sort": SORT,
"projection": PROJECTION
}
}'
Result
-
Python
-
TypeScript
-
Java
-
curl
Deletes a document that matches the specified parameters, and returns a dictionary representation of the document.
If no document was found, returns None
.
The fields included in the returned document depend on the projection specified in the method.
Example response:
{'_id': 101, 'name': 'John Doe'}
Deletes a document that matches the specified parameters, and returns a promise that resolves to the document (WithId<Schema>
).
If no document was found, returns returns a promise that resolves to null
.
The fields included in the returned document depend on the projection specified in the method.
Example resolved response:
{_id: 101, name: 'John Doe'}
Deletes a document that matches the specified parameters, and returns the document (Optional<T>
).
If no document was found, returns returns Optional.empty()
.
The fields included in the returned document depend on the projection specified in the method.
Deletes a document that matches the specified parameters, and returns a dictionary representation of the document.
If no document was found, returns null
instead of a dictionary representation of the document.
The fields included in the returned document depend on the projection specified in the method.
The response also includes details about the success of the operation, such as the number of documents that were deleted.
Example response:
{
"data": {
"document": {
"_id":"101",
"name":"John Doe"
}
},
"status":{
"deletedCount": 1
}
}
Example response if no document was found:
{
"data":{
"document":null
},
"status":{
"deletedCount": 0
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Name | Type | Summary |
---|---|---|
|
|
Optional. An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Data API operators. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
|
Optional. Controls which fields are included or excluded in the returned document. For more information, see Projection operations. Default: The default projection for the collection.
Certain fields, like |
|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
|
|
Optional. The maximum time, in milliseconds, that the client should wait for the underlying HTTP request. Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the |
Name | Type | Summary |
---|---|---|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Data API operators. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
|
Optional.
The options for this operation. See the |
Name | Type | Summary |
---|---|---|
Optional. Controls which fields are included or excluded in the returned document. For more information, see Projection operations. Default: The default projection for the collection.
Certain fields, like |
||
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
||
|
Whether to include details about the success of the operation in the response. Default: False |
|
|
Optional. The maximum time, in milliseconds, that the client should wait for the underlying HTTP request. Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the |
Name | Type | Summary |
---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Data API operators. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
Optional.
The options for this operation. See the methods of the |
Method | Parameters | Summary |
---|---|---|
|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
|
|
Optional. Controls which fields are included or excluded in the returned document. For more information, see Projection operations. Default: The default projection for the collection.
Certain fields, like |
Use the findOneAndDelete
command with these parameters:
Name | Type | Summary |
---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Data API operators. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
|
|
Optional. Controls which fields are included or excluded in the returned document. For more information, see Projection operations. Default: The default projection for the collection.
Certain fields, like |
Examples
The following examples demonstrate how to delete a document in a collection.
Delete a document by ID
All documents have a unique _id
property. You can use a filter to find a document with a specific _id
, and then delete that document.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.find_one_and_delete(
{"_id": "101"}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.findOneAndDelete(
{ _id: "101" }
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = Filters.eq("_id", "101");
Optional<Document> result = collection.findOneAndDelete(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": {
"_id": "101"
}
}
}'
Delete a document that matches a filter
You can use a filter to find a document that matches specific criteria.
For example, you can find a document with an isCheckedOut
value of false
and a numberOfPages
value less than 300.
For a list of available filter operators and more examples, see Data API operators.
Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.find_one_and_delete(
{
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.findOneAndDelete(
{
$and: [
{ isCheckedOut: false },
{ numberOfPages: { $lt: 300 } }
],
}
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
Optional<Document> result = collection.findOneAndDelete(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": {"$and": [
{"isCheckedOut": false},
{"numberOfPages": {"$lt": 300}}
]}
}
}'
Delete a document that is most similar to a search vector
To find the document whose $vector
value is most similar to a given vector, use a sort with the vector embeddings that you want to match. For more information, see Perform a vector search.
Vector search is only available for vector-enabled collections. For more information, see Vector and vectorize.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.find_one_and_delete(
{},
sort={"$vector": [0.1, 0.2, 0.3]},
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.findOneAndDelete(
{},
{ sort: { $vector: [0.1, 0.2, 0.3] } },
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = new Filter();
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions()
.sort(new float[] {0.12f, 0.52f, 0.32f});
Optional<Document> result = collection.findOneAndDelete(filter, options);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": {},
"sort": {
"$vector": [0.1, 0.2, 0.3]
}
}
}'
Delete a document that is most similar to a search string
To find the document whose $vector
value is most similar to the $vector
value of a given search string, use a sort with the search string that you want to vectorize and match. For more information, see Perform a vector search.
Vector search with vectorize is only available for collections that have vectorize enabled. For more information, see Vector and vectorize.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.find_one_and_delete(
{},
sort={"$vectorize": "Text to vectorize"}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.findOneAndDelete(
{},
{ sort: { $vectorize: "Text to vectorize" } }
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = new Filter();
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions()
.sort("Text to vectorize");
Optional<Document> result = collection.findOneAndDelete(filter, options);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": {},
"sort": { "$vectorize": "Text to vectorize" }
}
}'
Delete a document after applying a non-vector sort
You can use a sort clause to sort documents by one or more fields.
For more information, see Sort clauses.
Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.constants import SortDocuments
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find a document
result = collection.find_one_and_delete(
{"metadata.language": "English"},
sort={
"rating": SortDocuments.ASCENDING,
"title": SortDocuments.DESCENDING,
}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Find a document
(async function () {
const result = await collection.findOneAndDelete(
{ "metadata.language": "English" },
{ sort: {
rating: 1, // ascending
title: -1 // descending
} }
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions()
.sort(Sorts.ascending("rating"))
.sort(Sorts.descending("title"));
Optional<Document> result = collection.findOneAndDelete(filter, options);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": { "metadata.language": "English" },
"sort": {
"rating": 1,
"title": -1
}
}
}'
Include only specific fields in the response
To specify which fields to include or exclude in the returned document, use a projection.
Certain fields, like $vector
and $vectorize
, are excluded by default and will only be returned if you specify that they should be included. Certain fields, like _id
, are included by default.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find a document
result = collection.find_one_and_delete(
{"metadata.language": "English"},
projection={"isCheckedOut": True, "title": True}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Find a document
(async function () {
const result = await collection.findOneAndDelete(
{ "metadata.language": "English" },
{ projection: { isCheckedOut: true, title: true} },
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
import java.util.Optional;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Find a document
Filter filter = Filters.eq("metadata.language", "English");
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions()
.projection(Projections.include("isCheckedOut", "title"));
Optional<Document> result = collection.findOneAndDelete(filter, options);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": {"metadata.language": "English"},
"projection": {"isCheckedOut": true, "title": true}
}
}'
Exclude specific fields from the response
To specify which fields to include or exclude in the returned document, use a projection.
Certain fields, like $vector
and $vectorize
, are excluded by default and will only be returned if you specify that they should be included. Certain fields, like _id
, are included by default.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find a document
result = collection.find_one_and_delete(
{"metadata.language": "English"},
projection={"isCheckedOut": False, "title": False}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Find a document
(async function () {
const result = await collection.findOneAndDelete(
{ "metadata.language": "English" },
{ projection: { isCheckedOut: false, title: false} },
);
console.log(result);
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
import java.util.Optional;
public class FindOneAndDelete {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Find a document
Filter filter = Filters.eq("metadata.language", "English");
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions()
.projection(Projections.exclude("isCheckedOut", "title"));
Optional<Document> result = collection.findOneAndDelete(filter, options);
System.out.println(result);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOneAndDelete": {
"filter": {"metadata.language": "English"},
"projection": {"isCheckedOut": false, "title": false}
}
}'
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.