Cloudant query to return records where 2 fields are equal

杀马特。学长 韩版系。学妹 提交于 2019-12-12 03:56:56

问题


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

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

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)

I would like to search where parentDocId == docId.

Any ideas?

p.s. I am using Swift


回答1:


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

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

you can create a view like this

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}"
   }
  }
 }

and query it:

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

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.



来源:https://stackoverflow.com/questions/42735442/cloudant-query-to-return-records-where-2-fields-are-equal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!