Marklogic|NodeJS API - 查询字段的属性

Vic*_*lan 3 marklogic nosql node.js

我有一个 json 结构,类似于

{
   foo:"bar",
   keyPhrases:[
      {key: "random thing", value: 5},
      {key: "another random", value: 3}
   ]
}
Run Code Online (Sandbox Code Playgroud)

如何在 keyPhrases.key 上进行单词查询?我试过

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

或者

qb.word(qb.property('keyPhrases.key'), 'random')
Run Code Online (Sandbox Code Playgroud)

它不起作用。有任何想法吗?我知道这对于 QBE 是可能的,但对于 Marklogic NodeJS API,你无法指定集合,而我需要这样做。

Dav*_*sel 5

您需要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);
});
Run Code Online (Sandbox Code Playgroud)

或者,您可以构建一个基于路径的字段并对其进行查询。