Announcement Announcement Module
Collapse
No announcement yet.
MongoDB @Query and $and operator issues Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB @Query and $and operator issues

    I'm trying to write JSON based mongo queries in my repository class which ends up with an "$and expression must be a nonempty array" exception. I'm using spring-data-mongodb v1.0.1.RELEASE with spring v3.1.0.RELEASE.

    Take the following example (I know it's not necessary to use @Query to achieve this, but it's to prove the concept):
    Code:
    @Query("{ $and: [ { requesterId: ?0 }, { receiverId: ?1 } ] }")
    public boolean hasRelationship(String user1Id, String user2Id);
    When I debug I can see the following query in MongoTemplate:
    Code:
    $and={ "0" : { "requesterId" : "4f4373aa300439b4e215bff6"} , "1" : { "receiverId" : "4f4373aa300439b4e215bff7"}}
    The "0" and "1" seems kind of odd to me?

    Full stacktrace is:
    Code:
    org.springframework.data.mongodb.UncategorizedMongoDbException: $and expression must be a nonempty array; nested exception is com.mongodb.MongoException: $and expression must be a nonempty array
    	at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:70)
    	at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1538)
    	at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1449)
    	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1259)
    	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1248)
    	at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:471)
    	at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:458)
    	at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:450)
    	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery$SingleEntityExecution.execute(AbstractMongoQuery.java:199)
    	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:92)
    	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:301)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

  • #2
    I have exactly the same problem with data-mongodb v1.0.2.SNAPSHOT and spring v3.1.1.RELEASE. When debugging, I can see in method http://static.springsource.org/sprin...sistentEntity) that query (DBObject) parameter is
    Code:
    { "$and" : [ { "aliases.canonical" : "vienne"} , { "type.code" : "LOCATION-TYPE-5"}]}
    and the returning DBObject is
    Code:
    { "$and" : { "0" : { "aliases.canonical" : "vienne"} , "1" : { "type.code" : "LOCATION-TYPE-5"}}}
    I think this is bug.

    Comment

    Working...
    X