Query sync gateway buckets using N1QL

左心房为你撑大大i 提交于 2019-12-13 13:15:22

问题


I wanted to know if it's possible to query the sync gateway buckets using N1QL? Does it behave as a normal couchbase bucket or because of the metadata that sync gateway adds, is it possible to query it only through Rest APIs?

Currently I have a webhooks handler, which keeps a replica of the documents residing under sync gateway buckets. I need to do some aggrgations which need to be pushed back to clients. So, can I do all this heavy lifting directly trhough n1ql on sync gateway or using webhooks which does the aggregations and simply pushes the updated docs to sync gateway is the right option?

PS: The webhooks+Rest APIS option works perfectly for me currently. Just wanted to understand if this hop is necessary or not?


回答1:


Yes, it is possible to query the sync gateway using N1QL - you just can't change it (update/delete/insert), as it would break the revisions' metadata.

You need to ignore the documents with IDs starting with _sync: and the _sync property of each document, which contains internal metadata. The remaining attributes are your usual document.

Example:

select db.* from db where meta().id not like '_sync:%'

Result:

[
  {
    "_sync": {
      "history": {
        "channels": [
          null,
          null
        ],
        "parents": [
          -1,
          0
        ],
        "revs": [
          "1-b7a15ec4afbb8c4d95e2e897d0ec0a2e",
          "2-919b17d3f418100df7298a12ef2a84bb"
        ]
      },
      "recent_sequences": [
        6,
        7
      ],
      "rev": "2-919b17d3f418100df7298a12ef2a84bb",
      "sequence": 7,
      "time_saved": "2016-05-04T18:54:26.952202911Z"
    },
    "name": "Document with two revisions"
  }
]

Ignoring the _sync attribute:

select name from db where meta().id not like '_sync:%'

Result:

[
  {
    "name": "Document with two revisions"
  }
]

In Couchbase 4.5 (BETA as of today) we can use the object_remove function - although I'd avoid it in favor of the previous more explicit syntax.

select object_remove(db, '_sync') from db where meta().id not like '_sync:%'

Result:

[
  {
    "$1": {
      "name": "Document with two revisions"
    }
  }
]

I don't know what's your setup currently, but AFAIK, it's perfectly fine to keep querying the bucket throught N1QL while using the REST API for the data changes.



来源:https://stackoverflow.com/questions/36871589/query-sync-gateway-buckets-using-n1ql

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