MongoDB, Java - From JSON Query to Document

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!

Answers


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))
                        )
                )
            )
    )
   )
)   

Need Your Help

Magit revert single file

git emacs magit

The question is about Magit major mode for Emacs function and less so about how to do this via command line interface.

C# Using a single event for increment and decrement

c# events

I have a recursive function and two events (Going_in and Coming_out).