Tutan Ramen
Tutan Ramen

Reputation: 1242

MarkLogic - node.js Client API - queryBuilder query array of IDs

This question is similar to: MarkLogic - XQuery - cts:element-range-query using variable length sequence or map

But this time I need to do the query using the queryBuilder in the node.js client API.

I have a collection of 100,000 records structured like this:

<record>
    <pk>1</pk>
    <id>1234</id>
</record>
<record>
    <pk>2</pk>
    <id>1234</id>
</record>
<record>
    <pk>3</pk>
    <id>5678</id>
</record>
<record>
<pk>4</pk>
    <id>5678</id>
</record>

I have setup a range index on id.

I want to write a query using the queryBuilder node.js client API that will allow me to pass in an array of IDs and get out a list of records.

It needs to: 1) query a specific collection 2) leverage the range indexes for performance

Upvotes: 0

Views: 161

Answers (1)

Tutan Ramen
Tutan Ramen

Reputation: 1242

Nevermind, I figured out the problem.

db.db.documents.query(
  q.where(
    q.collection('Records'),
    q.or(
        q.value('id', ['1', '2'])
    )
  ).slice(1, 99999999)
)

I originally tried to pass an array into q.value and I was only getting limited results (Got 10 when I expected 20). So I was under the impression that I was doing it wrong.

It turns out I just needed to slice the where clause to include everything. Apparently if you don't specify how much to take it defaults to 10.

Also note that when I tried .slice(0) which would have been preferred, I got an exception.

Upvotes: 2

Related Questions