Announcement Announcement Module
Collapse
No announcement yet.
Use of @Field with MongoRepository query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    public class Review implements Commentable, Serializable {
    
    	@Field("ct")
    	private Date createdTimestamp;
    	
    	@Field("t")
    	private String title;
    	
    	@Field("a")
    	private boolean active;
    	
    	@Field("m")
    	@DBRef
    	private Member member;
    	
    	@Field("cmnts")
    	private List<Comment> comments = new ArrayList<Comment>();
    	
    	@Field("type")
    	private int reviewType;
    
            ......
    I also have a MongoRepository as seen below:
    Code:
    public interface ReviewRepository extends MongoRepository<Review, String> {
    	public Page<Review> findByReviewType(int type, Pageable pageable);
    }
    The issue comes up when the query is executed:
    Code:
    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:
    org.springframework.data.mongodb.repository.MongoQueryCreator DEBUG - Created query { "reviewType" : 0}
    2011-10-01 18:27:36,545 [tomcat-http--3] org.springframework.data.mongodb.core.MongoTemplate 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!
    -Matt

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

    Code:
    @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!!!

    Comment


    • #3
      Just saw this was fixed for RC 1: https://jira.springsource.org/browse/DATADOC-291

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

      Comment

      Working...
      X