How do I count result from a query?

Thanks a million, brett!

This worked just as you stated.

Would you know how to extract that metric object from a promisified invocation? I experimented a bit once I got the callback implementation working as you instructed. I tried using bluebird .spread as well as the updated, native .then with function ([results, meta]) { …}

Any suggestions? Thanks again!

Btw, yours is the only answer I have found anywhere which includes an example. Very grateful to you.

Jay

Thanks, Eben. However, that isn’t useful. @brett19 provided a callback-style solution that worked for me. Would you know how to extract the metric object from a promisified invocation?

Any reason why Couchbase tied the query window count to ORDER BY only? It would be nice if the number of records matching the query criteria would return regardless of the presence or absence of an ORDER BY clause.

Thanks!

Jay

Hey @jgcoding,

Extracting the metric from a promisified version would not be possible unfortunately. The promisification process expects there to be only 2 parameters to the callback function (which resolve and reject respectively). In order to access the metrics, a special wrapper would need to be used which merges the meta data into the result data for when it is resolved.

An example wrapper callback would be something like this, though it depends on what promisification library is being used for how you would integrate this into it.

return new Promise((resolve, reject) => {
  bucket.query(..., (err, res, meta) => {
    if (err) {
      reject(err);
    } else {
      resolve({rows: res, meta: meta});
    }
  });
});

P.S. The new 3.0 SDK which is in development has full support for promises and async/await built in.

Cheers, Brett

Ah! I was close!. I will give this a whirl.

I have been using bluebird with couchbase but without the metrics object, of course. I have been applying “promisifyAll” to the required couchbase module and it all works great with the xxxAsync operators generated by bluebird.

Your generosity is appreciated!

Jay

CB 4.5.0 onwards COUNT(*) is optimized same as COUNT(1)
Currently using 7.1.5. Is this in the docs somewhere?

Indirectly says “regardless of value.”, As count required single argument one can use * or constant to count all input rows
.
if expression and depends on field it must be valued (not missing or not null).

You can do EXPLAIN it will reflected there

MB-18563

2 Likes