mongodb collation doesn't work insinde aggregation with facet stage

Viewed 1952

I want to sort my products after using aggregation, in my aggregation pipeline i use $sort inside $facet stage. Actually sorting was working just fine for all fields until i try to sort a string field(Name) ascending/descending.

Problem is sorting doesn't work properly in my language which is Turkish. So i am using Collation options.

I am using C#.

Normally i use Collation locale setting like below

PipelineDefinition<BsonDocument, Model> pipeline = new BsonDocument[]
{
   //some stages,

  new BsonDocument().Add("$sort", new BsonDocument().Add("sortingParameter", -1)),

   //some stages
};

  var options = new AggregateOptions() { Collation = new Collation("tr") };
  var cursor = _collection.Aggregate(pipeline, options); 
                                                                                                  

and it is working, no problem here.

But some of my queries more complex than above example, such as (below i am showing shortened BsonDocument version of my query)

db.getCollection('collName').aggregate([ 
{"$match": {    // match operations  }
}, 
 {"$facet": {
     "product_list": [
         {"$group": {"_id": { /some grouping parameters }}},
         { "$sort": { "_id.Name": -1.0 } },
         { "$project": { // some projects }} 
   ]}}])

for above query i use the same aggregate options

var options = new AggregateOptions() { Collation = new Collation("tr") };

but sorted results are incorrect. When i try to remove $match stage i don't know how but it works.

So, is there any way to solve this or is it impossible with this kind of query?

1 Answers

Instead of using collation-locale setting inside the AggregateOptions, if i create the collection with collation-locale setting, the query works properly.

Related