Announcement Announcement Module
No announcement yet.
findByExample in MongoDB Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • findByExample in MongoDB

    Hi Guys,

    I'm using spring data on top of mongoDB to store and query data.
    We had a findByExample method in our hibernate DAOs before,
    So I wonder how can I do the same thing in spring-data and mongodb without using extra library?


  • #2
    Have you learned the MongoDB query language yet? If not I highly recommend learning it, not just for coding purposes but if you are in the mongo shell and need to query some stuff.

    I found it odd at first and had to go through some pains, but once I got through the hump, it really helped. Especially understanding the $commands like $elemMatch and .$. when working with arrays. Alot of times that can come into play when querying by example.

    As far as Spring Data Mongo, here are some things I found in the docs

    1) Query(where("name").is("Joe")), Person.class));

    So the mongoOps (template class) has methods to find and findOne, where you can add a new Query. But you would have to build the predicates based on your example. This approach take a bit more code, kind of like the old days of sql String with concatination then removing the last 5 character to remove that " AND " that you always add. With lots of if statements

    Then in section 5.10 I found

    BasicQuery query = new BasicQuery("{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}");

    This still probably requires if statements, but uses mongos query language, which personally I like better than #1 above.

    I did not find in the documentation any statement that said that if you pass an example object and that object only has certain fields populated that it will create a query with only those properties. However, that is not to say that it might not be coded to already work that way. So you can try to do a Spring Data Repository with a derived query taking in the domain object

    So if you have a Person object partially populated and a method

    public List<Person> findByPerson(Person p);

    That might work. Have you tried that approach?

    If you have to query by example where you have a relationship with say a Set. I doubt, but don't know if that would even work, as the code in Spring Data Mongo to accomplish that can be very difficult. So with that case you might have to do #1 or #2 above.

    Good Luck



    • #3
      Thanks Mark,

      Actually I know its query language well,
      And as I know one way of doing it is to translate the example entity class to DBObject and then pass it to mongoDB to get the result,
      then we can push that function into BaseRepository and call findByExample in a generic fashion.
      I wonder if there is a built in class/method somewhere in spring-data to do that?

      In the following class, they are doing exactly the same thing:

      And I'll give that Spring Data Repository a try.



      • #4
        Cool. wasn't sure where you were at. So basically, give it a try, or maybe Oliver sees this post and I am sure he knows if that was implemented to only use properties of an entity instance that has data into a query by example.

        I tried looking through the Spring Data Mongo DB code, but couldn't find where that would be.