我试图在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)
有人做过这样的事吗?
尝试使用自定义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)