我的文档存储在 marklogic 中的 JSON 中,如下所示(我删除了我的案例中无用的属性):
{
documentId: '',
languages: [{
locale: 'en_UK',
content: {
translated: 'true',
}
}, {
locale: 'de_DE',
content: {
translated: 'false',
}
}, {...}],
}
edit:看来我的“无用”属性引起了一些问题。这是我的详细对象。
{
documentId: '',
/* 4 attrs */,
languages: [{
locale: 'en_UK',
attr: '',
content: {
/* 14 attrs */,
translated: true,
/* 2 or 4 attrs */,
}
}, {
locale: 'de_DE',
attr: '',
content: {
/* 14 attrs */,
translated: false,
/* 2 or 4 attrs */,
}
}, {...}
],
/* 0 or 2 attrs */
}
我尝试查找其中至少包含一个对象的所有文档语言 where 语言环境 = 'en_UK' and 内容.翻译 = true with
var query =
qb.where(
qb.directory('myDocuments'),
qb.scope(
qb.property('languages'),
qb.and(
qb.scope(qb.property('code'), qb.term('en_UK')),
qb.scope(qb.property('translated'), qb.term('true'))
),
qb.fragmentScope('properties')
)
);
and
qb.where(
qb.directory(myDocuments'),
qb.scope(qb.property('languages'),
qb.propertiesFragment(
qb.value(
qb.property('languages'),
qb.and(
qb.scope(qb.property('code'), qb.term('en_UK')),
qb.scope(qb.property('translated'), qb.term('true'))
)
)
)
)
)
但在这两种情况下,查询都会返回与整个文档中的两个条件匹配的文档,而不是在每个对象中语言 array.
我阅读了文档,但没有找到任何内容。您对我如何进行查询有什么想法吗?
edit:我尝试了近查询,但它不起作用。它与好的文档不匹配。
qb.where(
qb.directory(config.marklogicConfiguration.product),
qb.scope(qb.property('languages'),
qb.near(
qb.and(
qb.scope(qb.property('code'), qb.term('ja_JP')),
qb.scope(qb.property('translatedInTheLanguage'), qb.term('true'))
),
1,
qb.weight(0),
qb.ordered(true)
)
)
)
我会问我是否可以改变我的对象结构。
edit2:最后我使用Xquery请求得到了正确的结果。
xdmp:directory("/product/direcory/")/languages[code eq "ja_JP" and content/translated eq "true"] ! root(.)
就我而言,我使用 eq 来内容/翻译条件,因为我的布尔值存储为字符串。!root(.):返回整个对象,而不仅仅是符合条件的语言对象[代码 eq“ja_JP”和内容/翻译 eq“true”]