如何使用查询从CouchBaseLite Mobile查找数据?

时间:2017-06-22 07:15:29

标签: ios swift3 couchbase-lite

我的文件

let properties: [String : Any] = [
            "project_id":Task.project_id!,
            "title": title,
            "parent_id": parent_id,
            "weight": "1",
            "created_at": DateTime.DateTime(),
            "updated_at": DateTime.DateTime(),
            "type": "group",
            "tag_id":tagId,
            "owner": Session.username!,
        ]

我想根据条件找到数据。

我的意思是,我想获取parent_id不是nil("")或null的文档。  我正在使用此查询

func setupViewAndQuery() {

    let listsView = database.viewNamed("list/GroupDropDown")

    if listsView.mapBlock == nil {
        listsView.setMapBlock({ (doc, emit) in
            if let type: String = doc["type"] as? String, let name = doc["title"], let parent_id = doc["parent_id"] as? String, let created  = doc["created_at"], let updated = doc["updated_at"], let Id = doc["_id"]
                , type == "group", parent_id != "" {
                emit([name,parent_id,created,updated,Id], nil)
            }
        }, version: "1.0")
    }

    listsLiveQuery = listsView.createQuery().asLive()
    listsLiveQuery.addObserver(self, forKeyPath: "rows", options: .new, context: nil)
    listsLiveQuery.start()
}

但此函数返回所有数据。怎么做这个任务?

1 个答案:

答案 0 :(得分:1)

您可以创建一个仅在parent_id不为nil时才会发出条目的视图。

或者,您可以使用groupLevel查看可以提供您想要的内容。这将涉及使用reduce函数,并且似乎不太适合您的结构。这需要对地图功能进行一些修改。

否则,您可以在迭代查询结果时过滤掉行。