Tutan Ramen
Tutan Ramen

Reputation: 1242

MarkLogic - Node.js Client API - QueryBuilder - Need page of data and full count for query

If I have the following queryBuilder query in the node.js client API:

db.client.documents.query(
  q.where(
    q.collection('records'),
    q.or(
        q.value('id', [1,2,3,4])
    )
  ).slice(0, 50)
)

This would give me the first 50 records related to this query. It would also give me a count of 50, even if there are 1000 records related to this query.

If I do a query with:

.withOptions({categories: 'none'})

I can see the real count for the query.

Is there a built-in option to give me a single page of data AND get the full count for the query?

Upvotes: 3

Views: 278

Answers (2)

Dave Cassel
Dave Cassel

Reputation: 8422

This will do what you're looking for:

db.documents.query(
  qb.where(
    qb.term('apple')
  )
    .slice(1, 10)
    .withOptions({categories: 'content', metrics: true})
).result()
.then(function(docs) {
  console.log('I have ' + docs.length + ' docs');
})
.catch(function(error) {
  console.log('something went wrong');
});

In the then function, docs will be an array of length 11. The first item in the array will be the metrics:

{
  "metrics": {
    "query-resolution-time": "PT0.00179S",
    "snippet-resolution-time": "PT0.00505S",
    "total-time": "PT0.008708S"
  },
  "page-length": 10,
  "results": [],
  "snippet-format": "snippet",
  "start": 1,
  "total": 20
}

The results array will have the snippets. Items 1 through 10 of the array will have the full contents of the document.

Upvotes: 2

vish.Net
vish.Net

Reputation: 962

In order to support paging, you would have to first call .withOptions({categories: 'none'}) to get the total count of records and then .withOptions({categories: 'content'}) to get the actual content. Following is very nice article on paging:

http://www.tamas.io/marklogic-and-node-js/

As per my knowledge there is no built-in way to fetch both total count and data together.

Upvotes: 4

Related Questions