gpt4 book ai didi

java - MongoDB、Java - 从 JSON 查询到文档

转载 作者:行者123 更新时间:2023-12-01 18:05:56 24 4
gpt4 key购买 nike

嘿,

我需要一些帮助将以下 MongoDB 查询转换为 MongoDB Java 驱动程序查询。

请注意,该查询有效。

db.days.aggregate([
{ $match: { 'day' : 'March_1'}},
{ $project: {
_id : 0,
day: 1,
events: {$filter: {
input: '$events',
as: 'event',
cond: {$eq: ['$$event.year', '2002']}
}}
}}
])

我的尝试是这样的,但失败了,我需要你的帮助。

Document query = new Document("$match", new Document("day", day)).
append("$project", new Document("_id", 0).
append("day", 1).
append("events", new Document("$filter", new Document(
"input", "$" + category).
append("as", "event").
append("cond", new Document("$eq", Arrays.asList("$$event.year", year))))));

我收到的错误是

"{ "ok" : 0.0, "errmsg" : "A pipeline stage specification object must contain exactly one field.", "code" : 16435 }"

非常感谢!

最佳答案

不要将 $match 和 $project 放在同一个对象中,使用列表

AggregateIterable<Document> iterable = collection.aggregate(
asList(
new Document("$match", new Document("day", day)),
new Document("$project",
new Document("_id", "0")
.append("day", 1)
.append(
"events",
new Document(
"$filter",
new Document("input", "$events")
.append("as", "event")
.append(
"cond",
new Document("eq", Arrays.asList("$$event.year", year))
)
)
)
)
)
)

关于java - MongoDB、Java - 从 JSON 查询到文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423731/

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