首页 > 解决方案 > 如何使用排序选项查询 MongoDB 聚合

问题描述

我创建了一个“品牌”集合,然后collation: { locale: "en", strength: 2}在“标题”字段上添加了一个索引。但是当我尝试使用这种排序规则查询它时,它不能按预期工作:

const query = brand => mongodb.db("my-db").collection("brands")
    .aggregate([
      { $match: { title: brand } },
      { $project: { _id: 0, total: 1, categories: { $slice: [ "$categories", 5 ] }, tags: { $slice: [ "$tags", 5 ] } } }
    ], { collation: { locale: 'en', strength: 2 } })
    .asArray();

所以调用brand('Test')会给出结果,但brand('test')会给出一个空列表。

当我使用 Atlas Aggregation Builder 尝试相同的查询时,两个查询都会给出相同的(非空)结果。有任何想法吗?

标签: mongodbmongodb-querymongodb-atlasmongodb-stitch

解决方案


推荐阅读