相关疑难解决方法(0)

使用 C# 驱动程序从 MongoDB 集合的文本查询中检索相关性有序结果

我正在尝试对集合进行文本查询并按文本匹配顺序检索结果。文档很好地解释了如何在 shell 中执行此操作:

db.articles.find(
   { status: "A", $text: { $search: "coffee cake" } },
   { score: { $meta: "textScore" } }
).sort( { date: 1, score: { $meta: "textScore" } } )
Run Code Online (Sandbox Code Playgroud)

但它需要将附加score字段从 find投影到排序中。

在 C# 中,我有一个如下所示的函数:

public IEnumerable<T> TextSearch<T>(MongoCollection<T> coll, string text) {
    var cursor = coll.Find(Query.Text(text))
        .SetSortOrder(SortBy<T>.MetaTextScore(???));
    foreach(var t in cursor) {
        // strip projected score from value
        yield return t;
    }
}
Run Code Online (Sandbox Code Playgroud)

但我缺少如何将“textScore”值投影到我的结果中,以便我可以将列指定MetaTextScorein SetSortOrder

c# mongodb mongodb-query mongodb-.net-driver

2
推荐指数
1
解决办法
1847
查看次数

标签 统计

c# ×1

mongodb ×1

mongodb-.net-driver ×1

mongodb-query ×1