Victor Balan
Victor Balan

Reputation: 1482

Marklogic|NodeJS API - Query on attribute of a field

I have a json structure something like

{
   foo:"bar",
   keyPhrases:[
      {key: "random thing", value: 5},
      {key: "another random", value: 3}
   ]
}

How can i do a word query on keyPhrases.key ? I tried

qb.word('keyPhrases.key', 'random')

or

qb.word(qb.property('keyPhrases.key'), 'random')

and it does not work. Any ideas? I know this is possible with a QBE but for the Marklogic NodeJS API you can`t specify a collection, and I need to.

Upvotes: 3

Views: 160

Answers (1)

Dave Cassel
Dave Cassel

Reputation: 8422

You need qb.scope().

var ml = require('marklogic');
var conn = require('./config.js').connection;
var db = ml.createDatabaseClient(conn);
var qb = ml.queryBuilder;

db.documents.query(
  qb.where(
    qb.collection('test'),
    qb.scope(qb.property('keyPhrases'), qb.word('key', 'random'))
  )
  .withOptions({metrics: true})
).result()
.then(function(docs) {
  console.log('This search found: ' + JSON.stringify(docs[1]));
})
.catch(function(error) {
  console.log('something went wrong: ' + error);
});

Alternatively, you could build a path-based field and query on that.

Upvotes: 5

Related Questions