Mongo Helpers - Filters.in() within Filters.expr()

Posted this to stackoverflow, but felt a cross-post here was sensible.

One thing I’ve noticed is that Filters.in doesn’t work and I have to use new Document("$in", Arrays.asList(userId, "$owners))

Is there a separate helper function for “Aggregate in”, and are there any other errors or improvements I could make?


The aim is

  • In the batches table is an entry per list of ‘primary batch for this list’
  • In the table of list items is a large collection of lists
  • I want
    • All the items with a particular status
    • Where the given userId is in the owners or readers
    • And where it is in the primary batch

Here’s my pipeline, which was generated in new Document() style in Compass, but I altered to use helpers for readability.

MongoCollection<Document> batches = mongoTemplate.getCollection("batches")

Bson lookup = Aggregates.lookup("listItems", // from
    Arrays.asList(
       new Variable<>("listId", "$listId"),
       new Variable<>("batchId", "$latestBatch")),  // let
    Arrays.asList(
       Aggregates.match(Filters.expr(Filters.eq("status"), statusVar))),
       Aggregates.match(Filters.expr(Filters.or(
           Filters.in("owners", userId),
           Filters.in("readers", userId)
       ))),
       Aggregates.match(Filters.expr(Filters.and(
           Filters.eq("listId", "$listId"),
           Filters.eq("batch", "$batchId")
       )))
    ), // pipeline
    "listItems"); // as

Bson project = Aggregates.project(new Document("listItems", 1L));

batches.aggregate(Arrays.asList(lookup, project));

First, I do not use builders because I like to use the same code in mongosh, nodejs or Java. And I usually do not like abstraction layers.

If you look at the documentation you will see there is 2 versions of $in:

Standard query

or using aggregation $expr

As you see the syntax is quite different. You call Filters.in inside Filters.expr so I suspect that the $expr version has to be used. However from the Filters.in documentation I looks like it generates the symple version.