Announcement Announcement Module
Collapse
No announcement yet.
MongoDB: Extra quotes being added to @Query values and fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB: Extra quotes being added to @Query values and fields

    I am trying to use the @Query annotation and it is adding extra quotation marks into my query. This breaks queries. Here is an example:

    Code:
    public interface OrderSetRepository extends MongoRepository<OrderSet, ObjectId> {
    
        @Query(value = "{ library : ?0 }", fields = "{ ?1 }")
        Page<OrderSet> findByLibraryPageableWithProjections(LibraryEnum libraryEnum, String projectionsJSON, Pageable pageable);
    }
    
    
    public class OrderSetRepositoryTest extends TestCase {
    
        @Test
        public void someTest() {
            Page<OrderSet> orderSetsPage3 = orderSetRepository.findByLibraryPageableWithProjections(LibraryEnum.ELSEVIER, "orderSetBody : 0",
                    new PageRequest(0, 5, new Sort(new Sort.Order(Sort.Direction.ASC, "topic"))));
        }
    }
    Produces the following:
    Code:
    com.mongodb.util.JSONParseException: 
    { "orderSetBody : 0" }
    See that? It wrapped my whole string in double quotes. So I have to make my query look like so:
    Code:
    "orderSetBody\" : \"0"
    Which produces the following:
    Code:
    { "orderSetBody" : "0" }
    Which seems correct. But its not. The quotes make the projection fail and it returns that part of the object instead of not including it. Also, it is really annoying to have to compose my strings that way - I should be able to pass my JSON as-is.

    I am pretty sure this is a bug. Just wanted to post it here first before I go to JIRA. Let me know if I am doing it wrong or if there is a better way or if there is a fix in place.

    Thanks.

    Charlie
Working...
X