No ID returned N1QL

Hmm, hi,

What do I have to do to return the document’s id with N1QL?

In the following code, the document’s id is not returned.

$cluster = new CouchbaseCluster("couchbase://");
$bucket = $cluster->openBucket("taxonomy");
  $query = CouchbaseN1qlQuery::fromString("SELECT * FROM taxonomy WHERE type = 'Kingdom'");
  $rows = $bucket->query($query);
  foreach($rows->rows as $v) {
    echo "<option value='someid'>".$v->taxonomy->name."</option>";

Should I create an ID field and generate a random id myself?
My document looks like:

id content
animalia {“type”:“Kingdom”,“name”:“Animalia”}
fungus {“type”:“Kingdom”,“name”:“Fungus”}

And I ran the following code as is from the PHP SDK Docs once to make N1QL works (but I don’t quite understand it):

try {
    // Do not override default name, fail if it is exists already, and wait for completion
    $bucket->manager()->createN1qlPrimaryIndex('', false, false);
    echo "Primary index has been created\n";
} catch (CouchbaseException $e) {
    printf("Couldn't create index. Maybe it already exists? (code: %d)\n", $e->getCode());

After a stupid question on Stackoverflow, I finally understood how to manage and store my data, but I really need IDs haha,
Thanks for your help!

you can get document’s id by META() function, for example

SELECT META().id,taxonomy.* FROM taxonomy LIMIT 1;

will return document and document’s id.

1 Like

@atom_yang thank you so much! I’ve been looking everywhere! It appears that I missed this though.

Sorry and thank you!