invirt.data.mongodb.collection.kt Maven / Gradle / Ivy
package invirt.data.mongodb
import com.mongodb.client.model.ReplaceOptions
import com.mongodb.kotlin.client.MongoCollection
import org.bson.conversions.Bson
fun MongoCollection.findOne(filter: Bson): T? {
val list = find(filter).toList()
if (list.size > 1) {
throw IllegalStateException("More than one document found for filter $filter")
}
return list.firstOrNull()
}
fun MongoCollection.get(id: String): T? {
return findOne(byId(id))
}
fun MongoCollection.delete(id: String): Boolean {
return deleteOne(byId(id)).deletedCount == 1L
}
/**
* Essentially an upsert operation
*/
fun MongoCollection.save(id: String, entity: T): T {
replaceOne(byId(id), entity, ReplaceOptions().upsert(true))
return entity
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy