Get the document along with the id


#1

I have stored the document in bucket and the key is document id and value is json object .My document content doesn’t have the doc id.

Now I want get the document id along with document content. Could you please let me know what is the correct query.


#2

META().id gives document id.

SELECT META(default).id, * FROM default;


#3

Thanks for your reply it s working with primary index not for secondary index.,As per the couch base admin its not recommend to create primary index for the bucket so we are not able to execute the above query.
Is there any possibility to execute this query on secondary index.


#4

What is your secondary index and query.

INSERT INTO default VALUES(“k0”,{“name”:“xyz”});
CREATE INDEX ix1 on default(name);
SELECT META().id, * FROM default WHERE name IS NOT NULL;
SELECT META().id, * FROM default WHERE name = “xyz”;
SELECT META().id, * FROM default WHERE name >= “xyz”;


#5

Thanks, its working.

Could you please let us know , Can we create the doc id as part of json object . Is it recomanded or not

We are seeing couple of options like some documents does nt have the doc id and some are having . Please let us know the approach.

Thanks for your reply.


#6

If you need document id use META().id you will get it.


#7

@veerareddy.boddapati- Years ago, we made the design decision to keep the key/id separate from the document body. This is in part because Couchbase supports some use cases where the value may not be JSON and can be arbitrary bytes specified by the app developer. The other reason is that the identification of a node responsible for a document without needing to do any across-the-wire index lookups gives Couchbase a great performance advantage for those (very common!) use cases.


#8

Our request doesn’t have the document id and we have some of the fields as part of the document in our request.

We are using N1QL query to fetch the data based on the request. But we are seeing its taking more time to fetch the data. I s there any other way to get more performance.


#9

Please post the EXPLAIN of the query.


#10

SELECT * FROM bucketname WHERE type=“type” AND name=“name”

@here we are fetching the data on fields not document id. and its taking lot of time.


#11

Please post the EXPLAIN so that we know what type of index you have.

Try the following index.

CREATE INDEX ix1 ON bucketname(name) WHERE type = "type";

Also The following has details how to design the index for query https://dzone.com/articles/n1ql-a-practicle-guide