Retrieve xattrs._sync.rev via n1ql, Java SDK or admin rest api

Hi

here is an example meta data of a document:

{
  "meta": {
    "id": "<my-doc-id>",
    "rev": "39080-15a545e5924b00000000000000000000",
    "expiration": 0,
    "flags": 0,
    "type": "json"
  },
  "xattrs": {
    "_sync": {
      "rev": "61-44fbbf3426fe3eacc2c6565434b5beb4",
      "sequence": 114571699,
      "recent_sequences": [
        114571658,
        114571659,
        114571660
      ],
      "history": {
        "revs": [
          "54-c95813ad67a322dd51db77b4d4097030",
          "52-6981bd0458bf550542dd3840ffda0067"
        ],
        "parents": [
          5,
          58,
          31
        ],
        "channels": [
          [
            "<my-channel>"
          ],
          [
            "<my-channel>"
          ]
        ]
      },
      "channels": {
        "<my-channel>": null
      },
      "cas": "0x00004b92e545a515",
      "value_crc32c": "0x000ae289",
      "time_saved": "2019-06-05T12:14:58.255792257+02:00"
    }
  }
}

Is there any way to retrieve xattrs._sync.rev via n1ql, Java SDK or admin rest api? If so could you kindly provide an example?

Thanks!

Hey @benjamin_glatzeder

I’m curious why you need to do this, if you let us know what you need to do with the info we may we able to suggest other approaches.

This (untested, admittedly) Java SDK code should access that data:

        DocumentFragment<Lookup> result = bucket.lookupIn("doc-id")
                .get("_sync.rev", SubdocOptionsBuilder.builder().xattr(true))
                .execute();

        String content = (String) result.content(0);

In N1QL you can see xattrs using the meta() function. It needs to be covered so we need to create an index first. For eg -
create index id2 on default(c1,meta().xattrs._sync,c2).

With the explain we should get -
“expr”: “cover ((meta(default).xattrs._sync))”

select c1,meta().xattrs._sync,c2 from default where c1=5;

Should give something like this -
{
“_sync”: ,
“c2” : 3
}

If you need to explicitly select something within _sync you can specify that.
select c1,meta().xattrs._sync.rev,c2 from default where c1=5;

Update
Following this thread: https://forums.couchbase.com/t/sg-rest-admin-api-gives-misleading-response/21823 it is recommended to use the SG GET document function to get the revision.

Old post

Thank you @isha and @graham.pople!

I went the n1ql route and the following query returns the latest revision:

SELECT meta().xattrs._sync.rev FROM <my_bucket> USE KEYS '<my_doc_id>';

The goal is to delete an attachment of a document. This SG REST API method [1] does not support removing an attachment but there is an open issue on Github [2]. To update the document and attachment I went with this method [3] which needs a revision as parameter. I believe with the above n1ql query the latest revision of the doc is returned. That is the revision I need to get the job done.

[1] https://docs.couchbase.com/sync-gateway/current/admin-rest-api.html#/attachment/put__db___doc___attachment_
[2] https://github.com/couchbase/sync_gateway/issues/1648
[3] https://docs.couchbase.com/sync-gateway/current/admin-rest-api.html#/document/put__db___doc_

Thanks!

i think that required way is the exact way to do that, may be i am wrong!