【问题标题】:mongodb collation doesn't work insinde aggregation with facet stagemongodb排序规则不适用于facet阶段的insinde聚合
【发布时间】:2020-11-09 13:07:29
【问题描述】:

我想在使用聚合后对我的产品进行排序,在我的聚合管道中,我在 $facet 阶段使用 $sort。实际上排序对所有字段都很好,直到我尝试对字符串字段(名称)升序/降序进行排序。

问题是排序在我的土耳其语中无法正常工作。所以我正在使用排序选项。

我正在使用 C#。

通常我使用如下排序规则区域设置

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); 
                                                                                                  

它正在工作,这里没问题。

但是我的一些查询比上面的例子更复杂,例如 (下面我显示的是我的查询的缩短 BsonDocument 版本)

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

对于上述查询,我​​使用相同的聚合选项

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

但排序结果不正确。当我尝试删除 $match 阶段时,我不知道它是如何工作的。

那么,有没有办法解决这个问题,或者这种查询是不可能的?

【问题讨论】:

    标签: mongodb sorting aggregation-framework collation facet


    【解决方案1】:

    如果我使用 collat​​ion-locale 设置创建集合,而不是在 AggregateOptions 中使用 collat​​ion-locale 设置,则查询可以正常工作。

    【讨论】:

    • 请分享一个例子。
    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2013-06-29
    • 2017-08-03
    • 2021-04-20
    相关资源
    最近更新 更多