问题
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