Reputation: 175
Hy,
I need some help translating the following MongoDB Query into MongoDB Java driver query.
Please be advised that the query works.
db.days.aggregate([
{ $match: { 'day' : 'March_1'}},
{ $project: {
_id : 0,
day: 1,
events: {$filter: {
input: '$events',
as: 'event',
cond: {$eq: ['$$event.year', '2002']}
}}
}}
])
My try is this, but it failed and I need your help.
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))))));
The error that I am getting is
"{ "ok" : 0.0, "errmsg" : "A pipeline stage specification object must contain exactly one field.", "code" : 16435 }"
Thank you very much!
Upvotes: 0
Views: 1084
Reputation: 926
Don't place the $match and $project in the same object, use a list
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))
)
)
)
)
)
)
Upvotes: 5