swift - MongoKitten : Sorting by a generated field

标签 swift mongodb mongokitten

我正在尝试在 MongoKitten 中创建以下 mongo 语句,这是一个快速框架。

db.pages.find( {$and: [{$text: {$search: "ebay"}} , {"lang" : "en"} ] }, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})

在 swift 中,我有以下内容,但是我无法按生成的字段 Score

对其进行排序
let query: Query = [
            "$text": ["$search": "ebay" ],
            "lang" : ["$eq": "en"],
        ]

let projection: Projection = [
             "_id": .excluded,
            "url": "url",
            "$score": ["$meta" : "textScore"]
        ]

let sort : Sort = [
            "score": .descending
        ]

        let matchingEntities: CollectionSlice<Document> = try pages.find(query, sortedBy: sort, projecting: projection)

有人做过这样的事吗?

最佳答案

尝试使用自定义 SortOrder

let query: Query = [
            "$text": ["$search": "ebay" ],
            "lang" : ["$eq": "en"]
]
let projection: Projection = [
            "_id": .excluded,
            "url": "url",
            "score": ["$meta" : "textScore"]
]
let sort : Sort = [
            "score": .custom(["$meta" : "textScore"])
]
let matchingEntities: CollectionSlice<Document> = try pages.find(query, sortedBy: sort, projecting: projection)

关于swift - MongoKitten : Sorting by a generated field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855494/

相关文章:

ios - 在特定 View 上退出应用程序

ios - 使用 [indexPath.section][indexPath.row] 的下标使用不明确

mongodb - 存储与分离流相关的数据

swift - 如何从 MongoKitten 查询中提取文档数组

swift - 创建干净的 Vapor 项目(使用 Fluent)会导致 MongoKitten 运行时错误

ios - IESLight 可以在 SceneKit 中转换阴影吗?

Swift 联合处理 HTTP 状态码错误

php - 通知: Undefined offset: 1 in line 23

javascript - meteor 集合中的数字字段名称