【问题标题】:MongoKitten : Sorting by a generated fieldMongoKitten:按生成的字段排序
【发布时间】:2018-02-18 19:07:39
【问题描述】:

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

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)

有人做过这样的事吗?

【问题讨论】:

    标签: swift mongodb mongokitten


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 2018-08-30
      • 1970-01-01
      • 2020-06-17
      • 2012-11-08
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多