MongoKitten:按生成的字段排序

问题描述 投票:1回答:1

我试图在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)

有人做过这样的事吗?

swift mongodb mongokitten
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.