Announcement Announcement Module
Collapse
No announcement yet.
Criteria In not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Criteria In not working

    I'm having trouble once more. It seems like the IN operator of the Criteria API is not working. And then i have a little feature request: I need min() and max() which is currently not part of the criteria API. I read that this is often realized with MapReduce, so general support for MapRedeuce would be great too.

    TestCase:

    Code:
        @Test
        public void testQueryIn() {
            MyResource resource = createResource("foo", "text/plain", "TEST");
    
            resource = resourceRepository.save(resource);
    
            assertNotNull(resource.getId());
    
            MyResource findById = resourceRepository.findById(resource.getId());
            assertNotNull(findById);  // works
    
            Query query = new Query();
    
            query.addCriteria(Criteria.where("id").in(resource.getId()));
    
            List<MyResource> found = mongoTemplate.find("myResource", query, MyResource.class);
            assertNotNull(found);
    
            assertFalse(found.isEmpty()); // fails
    
            MyResource r = found.get(0);
    
            assertNotNull(r);
    
            assertEquals(resource.getId(), r.getId());
            assertEquals(resource, r);
    
            resourceRepository.delete(resource);
    
            MyResource foundAfter = resourceRepository.findById(resource.getId());
            assertNull(foundAfter);
    
        }

  • #2
    As a workaround i tried to add multiple or Queries, but this doesn't work too:

    Code:
        @Test
        public void testQueryOr() {
            MyResource resource1 = createResource("test.jpg", "image/jpg", "TEST");
            MyResource resource2 = createResource("test.jpg", "image/jpg", "PROD");
    
            resource1 = resourceRepository.save(resource1);
            resource2 = resourceRepository.save(resource2);
    
            assertNotNull(resource1.getId());
            assertNotNull(resource2.getId());
    
            List<MyResource> expected = Arrays.asList(resource1, resource2);
    
            Query query = new Query();
            query.addCriteria(Criteria.where("id").is(resource1.getId()));
    
            Query or = new Query();
            or.addCriteria(Criteria.where("id").is(resource2.getId()));
    
            query.or(or);
    
            List<MyResource> found = mongoTemplate.find("myResource", query, MyResource.class);
    
            assertNotNull(found);
            assertFalse(found.isEmpty()); // fails (empty)
            assertEquals(2, found.size());
            assertEquals(expected, found);
    
            resourceRepository.delete(resource1);
            resourceRepository.delete(resource2);
    
            assertNull(resourceRepository.findById(resource1.getId()));
            assertNull(resourceRepository.findById(resource2.getId()));
    
    
        }

    Comment


    • #3
      Try using "_id" in your query to see if you get some hits. We do try to convert between "id" and "_id" in the MongoTemplate but it's not always foolproof.

      I just added some debug output to the MongoTemplate find methods so if you turn on debugging for org.springframework.data.document.mongodb and use a recent snapshot build then you should see the actual query being used.

      Comment


      • #4
        That doesn't work too.

        here you can see the difference between repository.findById() and the custom query with in criteria.

        Might this be an issue because i am using in for an id?

        The repository is using "$oid" and not "$is"

        Code:
        10:58:16,266 DEBUG MongoTemplate:961 - find using query: { "_id" : { "$oid" : "4d9d7ca55768d7e98e377d69"}}
        10:58:26,579 DEBUG MongoTemplate:961 - find using query: { "_id" : { "$in" : [ "4d9d7ca55768d7e98e377d69"]}}

        Comment


        • #5
          Can you post the JSON content of those documents from the Mongo database?

          -Thomas

          Comment


          • #6
            I created a ticket, i hope thats ok.

            https://jira.springsource.org/browse/DATADOC-84

            Code:
            {
               "_id": ObjectId("4d9dba50d575d7e9ba42c31e"),
               "tags": [
                
              ],
               "loggingEnabled": false,
               "logs": [
                
              ],
               "state": "PROD",
               "version": 2,
               "size": 0,
               "lastUpdatedAt": "Thu, 07 Apr 2011 15: 21: 20 +0200",
               "createdAt": "Thu, 07 Apr 2011 15: 21: 20 +0200",
               "name": "test.jpg",
               "uuid": "395894f7-e0c5-44d2-991d-c38cd82b3558",
               "mimeType": "image\/jpg"
            }

            Comment

            Working...
            X