[英]MongoDB, Java - From JSON Query to Document
HY,
我需要一些帮助,将以下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))
)
)
)
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.