1

I have a collection with first name, last name and birth year.

[
    {
        "firstName": "John",
        "lastName": "Smith",
        "birthYear": 1990
    },
    {
        "firstName": "Jane",
        "lastName": "Smith",
        "birthYear": 1989
    },
    {
        "firstName": "John",
        "lastName": "Doe",
        "birthYear": 1990
    },
    {
        "firstName": "Jane",
        "lastName": "Doe",
        "birthYear": 1990
    }
]

And I trying to find records with birthYear=1990 and ((firstName=John AND lastName=Smith) or (firstName=Jane AND lastName=Doe)).

The result I want is the following

[
    {
        "firstName": "John",
        "lastName": "Smith",
        "birthYear": 1990
    },
    {
        "firstName": "Jane",
        "lastName": "Doe",
        "birthYear": 1990
    }
]

Is there a way to do this using spring-data-mongodb's Query and Criteria?

1 Answer 1

0

You may use Criteria query like

Query qa =
  Query.query(
      new Criteria()
          .orOperator(
              Criteria.where("firstName").is("John").and("lastName").is("Smith").and("birthYear").is(1990),
              Criteria.where("firstName").is("Jane").and("lastName").is("Doe")));

or you can write two jpa methods like findAllByFirstNameAndLastNameAndBirthYear(String firstName, String lastName, int birthYear) and findAllByFirstNameAndLastName(String firstName, String lastName) and combine both list together.

Sign up to request clarification or add additional context in comments.

Comments

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.