Couchbase Java SDK: N1QL queries that include document id

时间:2018-07-16 09:23:23

标签: java couchbase n1ql

I'm looking to perform a query on my Couchbase database using the Java client SDK, which will return a list of results that include the document id for each result. Currently I'm using:

Statement stat = select("*").from(i("myBucket"))
                 .where(x(fieldIwantToGet).eq(s(valueIwantToGet)));

N1qlQueryResult result = bucket.query(stat);

However, N1qlQueryResult seems to only return a list of JsonObjects without any of the associated meta data. Looking at the documentation it seems like I want a method that returns a list of Document objects, but I can't see any bucket methods that I call that do the job.

Anyone know a way of doing this?

1 个答案:

答案 0 :(得分:6)

您需要使用以下查询来获取文档ID:

Statement stat = select("meta(myBucket).id").from(i("myBucket")) .where(x(fieldIwantToGet).eq(s(valueIwantToGet)));

上面的代码将返回一个文档ID数组。