use super::Cursor;
use crate::{
bson::{Bson, Document},
error::Result,
options::{
AggregateOptions,
CountOptions,
DeleteOptions,
DistinctOptions,
DropCollectionOptions,
EstimatedDocumentCountOptions,
FindOneAndDeleteOptions,
FindOneAndReplaceOptions,
FindOneAndUpdateOptions,
FindOneOptions,
FindOptions,
InsertManyOptions,
InsertOneOptions,
ReadConcern,
ReplaceOptions,
SelectionCriteria,
UpdateModifications,
UpdateOptions,
WriteConcern,
},
results::{DeleteResult, InsertManyResult, InsertOneResult, UpdateResult},
Collection as AsyncCollection,
Namespace,
RUNTIME,
};
#[derive(Clone, Debug)]
pub struct Collection {
async_collection: AsyncCollection,
}
impl Collection {
pub(crate) fn new(async_collection: AsyncCollection) -> Self {
Self { async_collection }
}
pub fn name(&self) -> &str {
self.async_collection.name()
}
pub fn namespace(&self) -> Namespace {
self.async_collection.namespace()
}
pub fn selection_criteria(&self) -> Option<&SelectionCriteria> {
self.async_collection.selection_criteria()
}
pub fn read_concern(&self) -> Option<&ReadConcern> {
self.async_collection.read_concern()
}
pub fn write_concern(&self) -> Option<&WriteConcern> {
self.async_collection.write_concern()
}
pub fn drop(&self, options: impl Into<Option<DropCollectionOptions>>) -> Result<()> {
RUNTIME.block_on(self.async_collection.drop(options.into()))
}
pub fn aggregate(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
) -> Result<Cursor> {
let pipeline: Vec<Document> = pipeline.into_iter().collect();
RUNTIME
.block_on(self.async_collection.aggregate(pipeline, options.into()))
.map(Cursor::new)
}
pub fn estimated_document_count(
&self,
options: impl Into<Option<EstimatedDocumentCountOptions>>,
) -> Result<i64> {
RUNTIME.block_on(
self.async_collection
.estimated_document_count(options.into()),
)
}
pub fn count_documents(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<CountOptions>>,
) -> Result<i64> {
RUNTIME.block_on(
self.async_collection
.count_documents(filter.into(), options.into()),
)
}
pub fn delete_many(
&self,
query: Document,
options: impl Into<Option<DeleteOptions>>,
) -> Result<DeleteResult> {
RUNTIME.block_on(self.async_collection.delete_many(query, options.into()))
}
pub fn delete_one(
&self,
query: Document,
options: impl Into<Option<DeleteOptions>>,
) -> Result<DeleteResult> {
RUNTIME.block_on(self.async_collection.delete_one(query, options.into()))
}
pub fn distinct(
&self,
field_name: &str,
filter: impl Into<Option<Document>>,
options: impl Into<Option<DistinctOptions>>,
) -> Result<Vec<Bson>> {
RUNTIME.block_on(
self.async_collection
.distinct(field_name, filter.into(), options.into()),
)
}
pub fn find(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<FindOptions>>,
) -> Result<Cursor> {
RUNTIME
.block_on(self.async_collection.find(filter.into(), options.into()))
.map(Cursor::new)
}
pub fn find_one(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<FindOneOptions>>,
) -> Result<Option<Document>> {
RUNTIME.block_on(
self.async_collection
.find_one(filter.into(), options.into()),
)
}
pub fn find_one_and_delete(
&self,
filter: Document,
options: impl Into<Option<FindOneAndDeleteOptions>>,
) -> Result<Option<Document>> {
RUNTIME.block_on(
self.async_collection
.find_one_and_delete(filter, options.into()),
)
}
pub fn find_one_and_replace(
&self,
filter: Document,
replacement: Document,
options: impl Into<Option<FindOneAndReplaceOptions>>,
) -> Result<Option<Document>> {
RUNTIME.block_on(self.async_collection.find_one_and_replace(
filter,
replacement,
options.into(),
))
}
pub fn find_one_and_update(
&self,
filter: Document,
update: impl Into<UpdateModifications>,
options: impl Into<Option<FindOneAndUpdateOptions>>,
) -> Result<Option<Document>> {
RUNTIME.block_on(self.async_collection.find_one_and_update(
filter,
update.into(),
options.into(),
))
}
pub fn insert_many(
&self,
docs: impl IntoIterator<Item = Document>,
options: impl Into<Option<InsertManyOptions>>,
) -> Result<InsertManyResult> {
let docs: Vec<Document> = docs.into_iter().collect();
RUNTIME.block_on(self.async_collection.insert_many(docs, options.into()))
}
pub fn insert_one(
&self,
doc: Document,
options: impl Into<Option<InsertOneOptions>>,
) -> Result<InsertOneResult> {
RUNTIME.block_on(self.async_collection.insert_one(doc, options.into()))
}
pub fn replace_one(
&self,
query: Document,
replacement: Document,
options: impl Into<Option<ReplaceOptions>>,
) -> Result<UpdateResult> {
RUNTIME.block_on(
self.async_collection
.replace_one(query, replacement, options.into()),
)
}
pub fn update_many(
&self,
query: Document,
update: impl Into<UpdateModifications>,
options: impl Into<Option<UpdateOptions>>,
) -> Result<UpdateResult> {
RUNTIME.block_on(
self.async_collection
.update_many(query, update.into(), options.into()),
)
}
pub fn update_one(
&self,
query: Document,
update: impl Into<UpdateModifications>,
options: impl Into<Option<UpdateOptions>>,
) -> Result<UpdateResult> {
RUNTIME.block_on(
self.async_collection
.update_one(query, update.into(), options.into()),
)
}
}