Announcement Announcement Module
No announcement yet.
Use of @Field with MongoRepository query Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use of @Field with MongoRepository query

    I am trying to get an application up and running using Spring and MongoDB (3.1 along with 1.0.0.M4 respectively). I am going to be bound by memory constraints and need tomake sure the data is as small as possible so it can fit memory on the server without having to thrash the disk to fetch data when it is not found in memory. One of the lesson's learned from a friend of mine that has a MongoDB instance running in a production environment was to reduce the size of the attribute names in the datastore. This can easily be accomplished through the use of the @Field annotation as seen below:

    public class Review implements Commentable, Serializable {
    	private Date createdTimestamp;
    	private String title;
    	private boolean active;
    	private Member member;
    	private List<Comment> comments = new ArrayList<Comment>();
    	private int reviewType;
    I also have a MongoRepository as seen below:
    public interface ReviewRepository extends MongoRepository<Review, String> {
    	public Page<Review> findByReviewType(int type, Pageable pageable);
    The issue comes up when the query is executed:
    Page<Review> recentReviews = reviewRepository.findByReviewType(0, reviewsPageRequest);
    The Spring Mongo framework is not taking into account that there is a @Field annotation on the attribute and tries to query using the method name.
    Code: DEBUG - Created query { "reviewType" : 0}
    2011-10-01 18:27:36,545 [tomcat-http--3] DEBUG - find using query: { "reviewType" : 0} fields: null for class: class com.myapp.domain.Review in collection: review
    I can revert back to not using the @Field annotation and increase the attribute name in the datastore for those attributes that use the queries from the repository as in above, but this would not be optimal as I am going to have to squeeze out every byte I can. Looking into the code, I wonder if there is something that could be done to take into account the @Field annotation. Maybe the PartTreeMongoQuery could take the @Field annotation into account as it does have access to the MongoEntityInformation object that can be be checked to see if the @Field annotation is present.

    I have a workaround for this to just resort to using the full field name or rename the property getter/setter methods in the domain object to match the short name being used.

    Thanks for any help in advance!

  • #2
    So another workaround to use is to use the @Query annotation:

    @Query("{ 's.$id': ?0, type: ?1 }")
    public Page<Review> findByStadiumIdAndReviewType(String stadiumId, int type, Pageable pageable);
    Note the syntax I had to use in order for the DBRef to work correctly.

    Thanks again to the Spring mongo team for creating all of this!!!


    • #3
      Just saw this was fixed for RC 1:

      Thanks to the Spring team for the quick response. Next time I'll add a JIRA issue for something like this.