Oleg Gritsak
Oleg Gritsak

Reputation: 622

Mongo java api ignores nested or/and operators

I want to search for person by: name AND ( document OR birthdate).

Seems rather simple, but my query doens't return anything. Program runs without errors, but 0 records found. If I remove or and search by name only, it works.

List<Bson> fullname = new ArrayList<>();
List<Bson> bd = new ArrayList<>();

bd.add(eq("by", 1999));
bd.add(eq("bm", 9));
bd.add(eq("bd", 18));

fullname.add(eq("l", ln));
fullname.add(eq("f", fn));
fullname.add(eq("p", pn));

Bson filter = and(
                    and(fullname),
                    or(
                            eq(and(bd)),
                            eq("document", document)
                    )
            );

Maybe there is a better syntax for such queries?

Upvotes: 0

Views: 32

Answers (1)

mtj
mtj

Reputation: 3554

As far as I understand your variable naming, this should be:

 Bson filter = and(
                and(fullname),
                or(
                        and(bd),                  // no "eq" here
                        eq("document", document)
                )
        );

Upvotes: 1

Related Questions