swift - 限制从 Swift 中的 Deployd 查询返回的对象

标签 swift deployd

我使用 Deployd 作为我的 Swift 应用程序的后端。这就是我当前查询 JSON 数据的方式。我需要限制从查询返回的对象的数量。我将自己实现分页部分。我只是想知道如何在查询中嵌入下面提到的 $limit 方法。任何建议将不胜感激。

http://docs.deployd.com/docs/collections/reference/querying-collections.html#s- $limit-1416

来自 Deployd 的文档:

The $limit command allows you to limit the amount of objects that are returned from a query. This is commonly used for paging, along with $skip.

// Return the top 10 scores {
    $sort: {score: -1},
    $limit: 10 }
import Foundation

class ObjectHandler {
    var greetings: [initTable] = []

    init(filename: String) {
        //filter data
        let fileP = NSURL(string: "http://localhost:2403/users/me")
        let jsonD = NSData(contentsOfURL:fileP!)
        let jso = JSON(data: jsonD!, options: NSJSONReadingOptions.AllowFragments, error: nil)
        var id = jso["id"]


        let filePath = NSURL(string: "http://localhost:2403/postedjob")
        let jsonData = NSData(contentsOfURL:filePath!)
        let json = JSON(data: jsonData!, options: NSJSONReadingOptions.AllowFragments, error: nil)

        for (key: String, subJson: JSON) in json {

            var language:String?, link: String?, description:String?, greetingText: String?

            for (key1, value:JSON) in subJson {
                switch key1 {
                case "briefDes": language = value.string
                case "skill": link = value.string
                case "userId": description = value.string
                case "id": greetingText = value.string
                default: break
                }
            }

            let greeting = initTable(language: language, link: link, description: description, greetingText: greetingText)

            self.greetings.append(greeting)
            self.greetings = self.greetings.filter { $0.description == "\(id)"}
        }
    }

    func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{

        return [SelectJobConstant.GreetingOBJHandlerSectionKey: greetings.map { $0 as AnyObject }]
    } }

最佳答案

这就是我让它工作的方法:)

Alamofire.request(.GET, "http://localhost:2403/postedjob", parameters: ["$limit": 2, "$sort": ["userId":"-1"]])
        .responseJSON { _, _, JSON, _ in
            println(JSON)
    }

关于swift - 限制从 Swift 中的 Deployd 查询返回的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241615/

相关文章:

node.js - 无法启动已部署(MongoDB 启动失败)

javascript - $$hashKey 不能以 '$' 开头 Deployd

javascript - 使用 dpd -d 启动 MongoDB 失败

json - 如何快速读取本地JSON文件并输出JSON?

swift - 如何将多个键值对添加到字典 Swift

ios - 从自定义 UIViewController 导航到 TabBarController 没有 segue

ios - 将 Swift 添加到 Objective-C 项目编译错误

ios - 使用开关更改整个应用程序中文本、单元格和 View Controller 的颜色

javascript - 在 Ubuntu 上托管后查找 MongoDB 详细信息