0

In the Java mongo driver how do you do an or search?

In the mongo aggregation framework it is documented as $or. But when I try and use that as as match criteria it throws an invalid operation exception.

So I tried omitting the operation and using the following, (this is Groovy code but using the Java driver)

def r = getCollection(collectionName).aggregate(
        [$match: [transactionType: [or: [1, 2]]],
        [$group:
                [_id: null,
                        sum: [$sum: '$property']]
        ]
).results()

But that doesn't work either. I've also tried removing the operator entirely and using just a List of values, but that also doesn't work.

I've tried looking throw the Java Mongo driver documentation but haven't found any mention of this. Anyone know the correct way to do an or operation here?

1 Answer 1

2

I think the problem is the way you're constructing your "or" statement. Does the following work instead?

[$match: [$or: [transactionType: 1],
               [transactionType: 2]]],
[$group .....
Sign up to request clarification or add additional context in comments.

1 Comment

That works, with a minor change to make the $match object a map. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.