gpt4 book ai didi

java - 使用 $elemMatch 时如何投影第一个子文档以外的内容

转载 作者:行者123 更新时间:2023-12-02 12:11:29 24 4
gpt4 key购买 nike

我有一个包含以下形式文档的集合:

{
"_id" : { "$oid" : "67bg............"},
"ID" : "xxxxxxxx",
"senses" : [
{
"word" : "hello",
"lang" : "EN",
"source" : "EN_DICTIONARY"
},
{
"word" : "coche",
"lang" : "ES",
"source" : "ES_DICTIONARY"
},
{
"word" : "bye",
"lang" : "EN",
"source" : "EN_DICTIONARY"
}
]
}

我想查找与 lang=Xsource=Y 至少一种含义匹配的所有文档,并返回仅包含这些 sense< 的匹配文档 匹配 lang=Xsource=Y

我试过这个:

DBObject sensesQuery = new BasicDBObject();
sensesQuery.put("lang", "EN");
sensesQuery.put("source", "EN_DICTIONARY");
DBObject matchQuery = new BasicDBObject("$elemMatch",sensesQuery);

DBObject fields = new BasicDBOject();
fields.put("senses",matchQuery);

DBObject projection = new BasicDBObject();
projection.put("ID",1)
projection.put("senses",matchQuery);
DBCursor cursor = collection.find(fields,projection)

while(cursor.hasNext()) {
...
}

我的查询适用于匹配文档,但不适用于投影。以上面的文档为例,如果我运行查询,我会得到以下结果:

{
"_id" : { "$oid" : "67bg............"},
"ID" : "xxxxxxxx",
"senses" : [
{
"word" : "hello",
"lang" : "EN",
"source" : "EN_DICTIONARY"
}
]
}

但我想要这个:

{
"_id" : { "$oid" : "67bg............"},
"ID" : "xxxxxxxx",
"senses" : [
{
"word" : "hello",
"lang" : "EN",
"source" : "EN_DICTIONARY"
},
{
"word" : "bye",
"lang" : "EN",
"source" : "EN_DICTIONARY"
}
]
}

我读到了有关聚合的内容,但我不明白如何在 MongoDB Java 驱动程序中使用它。

谢谢

最佳答案

您正在投影和过滤器上使用 $elemMatch 运算符。

来自the docs

The $elemMatch operator limits the contents of an field from the query results to contain only the first element matching the $elemMatch condition.

因此,您所看到的行为 elemMatch-in-a-projection 的预期行为。

如果您想投影符合过滤条件的文档中 senses 数组中的所有子文档,那么您可以使用:

projection.put("senses", 1);

但是,如果您只想投影那些与您的过滤条件匹配的子文档,那么 $elemMatch 将不适合您,因为它只返回与 $elemMatch< 匹配的第一个元素 条件。您的替代方案是使用聚合框架,例如:

db.collection.aggregate([
// matches documents with a senses sub document having the given lang and source values
{$match: {'senses.lang': 'EN', 'senses.source': 'EN_DICTIONARY'}},

// projects on the senses sub document and filters the output to only return sub
// documents having the given lang and source values
{$project: {
senses: {
$filter: {
input: "$senses",
as: "sense",
cond: { $eq: [ "$$sense.lang", 'EN' ], $eq: [ "$$sense.source", 'EN_DICTIONARY' ] }
}
}
}
}
])

这是使用 MongoDB Java 驱动程序的聚合调用:

Document filter = new Document("senses.lang", "EN").append("senses.source", "EN_DICTIONARY");

DBObject filterExpression = new BasicDBObject();
filterExpression.put("input", "$senses");
filterExpression.put("as", "sense");
filterExpression.put("cond", new BasicDBObject("$and", Arrays.<Object>asList(
new BasicDBObject("$eq", Arrays.<Object>asList("$$sense.lang", "EN")),
new BasicDBObject("$eq", Arrays.<Object>asList("$$sense.source", "EN_DICTIONARY")))
));

BasicDBObject projectionFilter = new BasicDBObject("$filter", filterExpression);

AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
new Document("$match", filter),
new Document("$project", new Document("senses", projectionFilter))));

for (Document document : documents) {
logger.info("{}", document.toJson());
}

结果输出是:

2017-10-01 17:15:39 [main] INFO  c.s.mongo.MongoClientTest - { "_id" : { "$oid" : "59d10cdfc26584cd8b7a0d3b" }, "senses" : [{ "word" : "hello", "lang" : "EN", "source" : "EN_DICTIONARY" }, { "word" : "bye", "lang" : "EN", "source" : "EN_DICTIONARY" }] }

更新 1:以下评论:

After a long period of testing, trying to understand why the query was slow, I noticed that the "$match" parameter does not work, the query should select only records that have at least one sense with source = Y AND lang = X and project them , but the query also returns me documents with senses = []

此过滤器:new Document("senses.lang", "EN").append("senses.source", "EN_DICTIONARY") 不会匹配没有 Senses 的文档 属性也不会匹配具有空 senses 属性的文档。为了验证这一点,我将以下文档添加到我自己的集合中:

{
"_id" : ObjectId("59d72a24c26584cd8b7b70a5"),
"ID" : "yyyyyyyy"
}

{
"_id" : ObjectId("59d72a3ac26584cd8b7b70ae"),
"ID" : "zzzzzzzzz",
"senses" : []
}

重新运行上面的代码,我仍然得到想要的结果。

我怀疑您关于上述代码不起作用的说法要么是误报,要么您正在查询的文档与我一直在使用的示例不同。

为了帮助您自己诊断此问题,您可以...

  • 与其他运营商合作,例如无论有没有 $exists 运算符,$match 阶段的行为都是相同的:

    new Document("senses", new BasicDBObject("$exists", true))
    .append("senses.lang", new BasicDBObject("$eq", "EN"))
    .append("senses.source", new BasicDBObject("$eq", "EN_DICTIONARY"))
  • 删除 $project 阶段以准确查看 $match 阶段生成的内容。

关于java - 使用 $elemMatch 时如何投影第一个子文档以外的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com