Marklogic|NodeJS API - Query on attribute of a field

邮差的信 提交于 2019-12-23 21:11:55

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/30573892/marklogicnodejs-api-query-on-attribute-of-a-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!