8

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)
   {
      filter = and(eq("field1", value),
                   gt("field2", value2));
   }
   else
   {
      filter = and(eq("field1", value),
                   gt("field2", value2),
                   eq("field3", optionalValue));
   }
Spuggiehawk
  • 180
  • 2
  • 4
  • 12
  • 5
    You can use Arraylist. Something like `List fields = new ArrayList<>(); fields.add(eq("field1",value)); fields.add( gt("field2", value2)); if(optionFieldRequired) { fields.add(eq("field3", optionalValue)); } Bson filter = and(fields);` – s7vr Apr 17 '18 at 13:01

1 Answers1

9

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));
}
glytching
  • 44,936
  • 9
  • 114
  • 120
  • Thanks. I thought this might be the case, but you can get some cool insights on Stack Overflow if you ask :-) – Spuggiehawk Apr 17 '18 at 11:56
  • 3
    `MongoClient.getDefaultCodecRegistry()` is replaced with `MongoClientSettings.getDefaultCodecRegistry()` – user52028778 Sep 27 '19 at 13:27
  • this answer is not valid, `append()` will append another new clause in parallel with previous `$and` clause. – Shu Dec 12 '21 at 16:16