本文介绍了Cloudant查询以返回两个字段相等的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以搜索字段的特定值。如何搜索与其他字段相等的字段?例如,

I can search for specific values for fields. How can I search for fields equal to other fields? For example,

我有 docId parentDocId ;如何搜索具有 docId == parentDocId 的记录?

I have docId and parentDocId; how do I search for records that have docId == parentDocId?

查询以搜索等于 123:

query to search for specific id which equals "123":

let query = ["parentDocId": ["$eq":"123"]]
let result = datastore.find(query, skip: 0, limit: 0, fields: nil, sort: nil)

我想搜索parentDocId == docId。

I would like to search where parentDocId == docId.

有什么想法吗?

ps我正在使用Swift

p.s. I am using Swift

推荐答案

无需使用Cloudant Query。假设您的文档具有以下结构

There's no need to use Cloudant Query. Assuming your docs have the following structure

{
 "_id": "2b7946de457b6fc9af3e3d29faa3fcba",
 "_rev": "3-088f847ef010504e7a5beb0c821d72ea",
 "parentDocId": "2b7946de457b6fc9af3e3d29faa3fcba",
 ...
}

您可以创建

PUT https://$USERNAME:$PASSWORD@$HOST/$DATABASE/_design/demo HTTP/1.1
Content-Type: application/json

 {
  "views": {
  "parent_child": {
   "map": "function (doc) {\n  if(doc._id === doc.parentDocId) {\n    emit(doc._id, 1);\n  }\n}"
   }
  }
 }

并查询:

GET https://$USERNAME:$PASSWORD@$HOST/$DATABASE/_design/demo/_view/parent_child HTTP/1.1

所有客户端库都应支持视图s因为它们是CouchDB / Cloudant中的基本概念。我不熟悉Swift,但是图书馆文档应该确定您需要调用的适当方法,以进行相当于我列出的常规HTTP的调用。

All client libraries should support views since they are a fundamental concept in CouchDB/Cloudant. I'm not familiar with Swift but the library documentation should identify the appropriate method you need to invoke to make a call that's equivalent to the general HTTP one I listed.

这篇关于Cloudant查询以返回两个字段相等的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 01:18