1

TLDR

Is there a way to limit queryByExample to a collection in NodeJS?

Problem faced

I have a complex query with some optional fields (i.e. sometimes some search fields will be omitted). So I need to create a query dynamically, e.g. in JSON. QueryByExample seems to be the right tool to use here as it gives me that flexibility to pass a JSON. However my problem is that I would like to limit my search to only one collection or directory.

e.g. I was hoping for something like

searchJSON = {
        title: { $word: "test" },
        description: { $word: "desc" }
};

//query
db.documents.query(qb.where(
    qb.collection("collectionName"),
    qb.byExample(searchJSON)
)).result()...

In this case searchJSON could have been built dynamically, for example maybe sometimes title may be omitted from the search.

This doesn't work because the query builder only allows queryByExample to be the only query. But I'd instead like to built a dynamic search query which is limited to a collection or directory.

1 Answer 1

2

At present, I think you would have to express the query with QueryBuilder instead of Query By Example using

qb.and([
  qb.collection('collectionName'),
  qb.word('title', 'test'),
  qb.word('description', 'desc')
  ])

See http://docs.marklogic.com/jsdoc/queryBuilder.html#word

That said, it should be possible for the Node.js API to relax that restriction based on the fixes in MarkLogic 9.0-2

Please file an issue on https://github.com/marklogic/node-client-api

Sign up to request clarification or add additional context in comments.

1 Comment

This works. I wasn't aware that the and method can take an array. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.