MongoKitten:按生成的字段排序

时间:2018-02-18 19:07:40

标签: swift mongodb mongokitten

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

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

在swift中我有以下内容但是我无法按生成的字段排序得分

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)

有人做过这样的事吗?

1 个答案:

答案 0 :(得分:1)

尝试使用自定义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)