我的文档存储在 marklogic 中的 JSON 中,如下所示(我删除了我的案例中无用的属性):
{
documentId: '',
languages: [{
locale: 'en_UK',
content: {
translated: 'true',
}
}, {
locale: 'de_DE',
content: {
translated: 'false',
}
}, {...}],
}
编辑:看来我的“无用”属性导致了一些问题。这是我的详细对象。
{
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 */
}
我尝试查找至少具有一个语言对象的所有文档,其中locale = 'en_UK'且content.translated = true与
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')
)
);
和
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'))
)
)
)
)
)
但在这两种情况下,查询都会返回与整个文档中的 2 个条件匹配的文档,而不是在 languages 数组的每个对象中。
我阅读了文档,但没有找到任何内容。您对我如何进行查询有什么想法吗?
编辑:我尝试了附近查询,但它不起作用。它与好的文档不匹配。
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 用于 content/translated 条件,因为我的 bool 值存储为字符串。 !root(.):返回整个对象,而不仅仅是匹配条件的语言对象[code eq "ja_JP"and content/translated eq "true"]
最佳答案
尝试使用 near-query , Location Qualifiers available in structured queries 之一。提供您的区域设置和翻译后的查询,指定distance: 1
和ordered: true
。请注意,这是否有效取决于您删除的“无用属性”所在的位置。
如果这不起作用,您可能需要在结构中引入另一层。
{
documentId: '',
languages: [{
item: {
locale: 'en_UK',
content: {
translated: 'true',
}
}
}, {
item: {
locale: 'de_DE',
content: {
translated: 'false',
}
}
}, {...}],
}
这不太漂亮,但它可以让你运行 container-query 。
关于Marklogic (Nodejs API) - 搜索与对象数组属性中的 2 个(或更多)条件匹配的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38573243/