Using $size aggregation in a transaction

52 views
Skip to first unread message

Robert Veres

unread,
Jan 27, 2022, 10:12:56 AM1/27/22
to kmongo
Hi, I'm new to KMongo and I would like to return number of elements in an Array in a specific document

something like this:

val result = chats.aggregate<Result>("[{\$match : {_id:'$chatId'}}, {\$project: { _id : 1, messageCount : {\$size: '\$messages'}}}]")

But, I need to do it in a transaction, because I use the result later.

But I can't find an aggregate function that would accept a ClientSession and a String. Only ClientSession and List<Bson>. Unfortunately from what I tried, it seems I can't use the size aggregation in Bson.

I'm probably overlooking something. Any ideas how can I do it?

Thank you

zigzago

unread,
Feb 12, 2022, 6:03:56 PM2/12/22
to kmongo
Hello,


fun <TResult : Any> MongoCollection<*>.aggregate(clientSession: ClientSession, vararg pipeline: String): AggregateIterable<TResult>

seems to fill your need - each string is a stage representation.

HTH
Reply all
Reply to author
Forward
0 new messages