MongoDB、Java - 从 JSON 查询到文档
MongoDB, Java - From JSON Query to Document
嗨,
我需要一些帮助将以下 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))
)
)
)
)
)
)
嗨,
我需要一些帮助将以下 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))
)
)
)
)
)
)