How to add to an existing MongoDB Bson Filter in Java

Spuggiehawk picture Spuggiehawk · Apr 17, 2018 · Viewed 12.6k times · Source

I'm using MongoDB 3.6.3 and the 3.6.0 Mongo & Bson drivers for Java.

Given the following filter:

import static com.mongodb.client.model.Filter.and;
import static com.mongodb.client.model.Filter.eq;
import static com.mongodb.client.model.Filter.gt;
.
.
.
   Bson filter = and(eq("field1", value),
                     gt("field2", value2));

I need to conditionally add another field to filter, effectively making it:

   Bson filter = and(eq("field1", value),
                     gt("field2", value2),
                     eq("field3", optionalValue));

Is there a way to do this by appending that field to filter, or do I have to create the filters separately? eg.

   Bson filter;
   if (optionFieldRequired)
   {
      Bson filter = and(eq("field1", value),
                        gt("field2", value2));
   }
   else
   {
      Bson filter = and(eq("field1", value),
                        gt("field2", value2),
                        eq("field3", optionalValue));
   }

Answer

glytching picture glytching · Apr 17, 2018

Filters.and() returns an instance of the private static class: Filters.AndFilter. There is no public method on AndFilter allowing you to change its state. So, if you want to append an additional filter after constructing this object you'll have to convert it into some other, mutable, form. For example; a BsonDocument.

The following code creates two BsonDocument instances, one by adding a filter to an existing set of filters and the other by creating all three filters at once. Both of these BsonDocument instances are identical and can be used in collection.find():

Bson filter = and(eq("field1", "value"), gt("field2", "value2"));
BsonDocument bsonDocument = filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry());

Bson optionalFilter = eq("field3", "optionalValue");
BsonDocument optionalBsonDocument = optionalFilter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry());

// now add the optional filter to the BsonDocument representation of the original filter
bsonDocument.append("field3", optionalBsonDocument.get("field3"));

Bson completeFilter = and(eq("field1", "value"), gt("field2", "value2"), eq("field3", "optionalValue"));
BsonDocument completeBsonDocument = completeFilter.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());

assertThat(completeBsonDocument, is(bsonDocument));

So, this solution is functional but I think it's harder to understand and less standard than wrapping the create call in a conditional block, like in your question ...

Bson filter;
if (!optionFieldRequired) {
  filter = and(eq("field1", value),
                    gt("field2", value2));
} else {
  filter = and(eq("field1", value),
                    gt("field2", value2),
                    eq("field3", optionalValue));
}