Don Dickinson
Don Dickinson

Reputation: 6258

Iterating through couchbase keys without a view

In couchbase, I was wondering if there was a way - WITHOUT using a view - to iterate through database keys. The admin interface appears to do this, but maybe its doing something special. What I'd like to is make a call like this to retrieve an array of keys:

$result = $cb->get("KEY_ALBERT", "KEY_FRED"); having the result be an array [KEY_ALEX, KEY_BOB, KEY_DOGBERT]

Again, I don't want to use a view unless there's no alternative. Doesn't look like its possible, but since the "view documents" in the admin appears to do this, I thought i'd double-check. I'm using the php interface if that matters.

Upvotes: 2

Views: 1822

Answers (3)

elwarren
elwarren

Reputation: 326

The couchbase PHP sdk does support multiget requests. For a list of keys it will return an array of documents.

getMulti(array $ids, array $cas, int $flags) : array

http://www.couchbase.com/autodocs/couchbase-php-client-1.1.5/classes/Couchbase.html#method_getMulti

Upvotes: 0

m03geek
m03geek

Reputation: 2538

I don't know about how couchbase admin works, but there are two options. First option is to store your docs as linked list, one doc have property (key) that points to another doc.

docs = [
  {
     id: "doc_C",
     data: "somedata",
     prev: "doc_B",
     next: "doc_D"
  },
  {
     id: "doc_D",
     data: "somedata",
     prev: "doc_C",
     next: "doc_E"
  }
]

The second approach is to use sequential id. You should have one doc that contain sequence and increment it on each add. It would be something like this:

docs = [
  {
     id: "doc_1",
     data: "somedata"
  },
  {
     id: "doc_2",
     data: "somedata"
  }    
  ...
]

In this way you can do "range requests". To do this you form array of keys on server side: [doc_1, doc_2 .... doc_N]and execute multiget query. Here is also a link to another example

Upvotes: 3

Tug Grall
Tug Grall

Reputation: 3520

Based on your comments, the only way is to create a simple view that emit only the id as par of the key:

function(doc, meta) {
  emit( meta.id );
}

With this view you will be able to create query with the various options you need : - pagination, range, ...

Note: you talk about the Administration Console, the console use an "internal view" that is similar to what I have written above (but not optimized)

Upvotes: 4

Related Questions