9

in Couchbase DB, is it possible to retrieve multiple documents using key prefix as query string, and it returns all the key-values which has key starting with supplied key prefix (like operator kind of thing)? without using Views or queries/indices.

I am designing my keys the way it is shown in Slide 51 of this presentation http://www.slideshare.net/Couchbase/couchbase-103-data-modeling

Anand
  • 4,523
  • 10
  • 47
  • 72
  • 1
    Is there a reason why you don't wish to use a view for this? Keep in mind that you can have the view or query only tell you the actualy IDs you need to fetch, and then you would fetch those using the normal KV APIs – Mark Nunberg Jun 01 '15 at 15:46

1 Answers1

14

If you don't want to use a view or n1ql query, there is no way to retrieve documents without knowing their exact keys. That is, you can only retrieve your prefix-based keys if you have a way to generate the possible keys on the client side in advance, e.g. User-1, User-2 ... User-n.

You can, however, do the sort of prefix query you're talking about in n1ql without creating any additional indexes, because with n1ql you will already have a primary index on all the document keys. So you can do something like "SELECT META(myBucket).id FROM myBucket WHERE META(myBucket).id LIKE "prefix%";

David Ostrovsky
  • 2,461
  • 12
  • 13
  • I am fine with one extra N1QL! It is very useful trick :) Thanks. I think I should prefer to use one extra N1QL instead of mantaining reference array of child documents in parent user:id {} object, which would require to be updated on each insertion/deletion of child. what do you think? – Anand Jun 04 '15 at 04:11
  • 1
    It depends on what your performance requirements are. Using a reference document to get the IDs of the child docs will make retrieval faster. However, maintaining what is essentially a manual index will be a lot more development work. I'd say if the performance is good enough, use the simplest approach, i.e. n1ql in this case. – David Ostrovsky Jun 04 '15 at 07:01
  • @DavidOstrovsky We are using exact same query you mentioned, however we are seeing no data unless we create a index. We are using couchbase version: 4.0.0-4051-1 – Saurabh Dec 27 '18 at 08:18