Announcement Announcement Module
Collapse
No announcement yet.
Mongo ISODate | Unable to search based on date (spring-data-mongodb 1.0.0.M4) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mongo ISODate | Unable to search based on date (spring-data-mongodb 1.0.0.M4)

    Hi,

    We recently migrated to 'spring-data-mongodb 1.0.0.M4' and we are facing issues when we try to execute a query based on date on mongo db. We are using the java.util.Date in our objects that we persist in Mongo and the insert works just fine. How ever, the date (java.util.date) based queries are not returning any results as it's getting stored in Mongo as ISODate.

    Can you please suggest how this needs to be handled?

    We are also facing issues when we search on the Object Id in the new version, as spring/mongo is trying to escape already escaped quotes. Any pointers/sample/links for the 1.0.0.M4 version could would be of great help

    Regards,
    Antony.

  • #2
    Unable to Search based on date in the latest version

    Hi,

    This is the test case that I'm using. Can some one please look in to this and suggest what can be done? Please let me know in case you need any other details?

    Code:
    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.MatcherAssert.assertThat;
    
    import java.util.List;
    
    import org.hamcrest.Matchers;
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormatter;
    import org.joda.time.format.ISODateTimeFormat;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.data.annotation.PersistenceConstructor;
    import org.springframework.data.annotation.Transient;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     */
    @RunWith(value = SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/dao-configuration-test.xml" })
    public class MongoDateStorageTest {
    
        @Autowired
        @Qualifier("mongoTemplate")
        private MongoTemplate mongoTemplate;
    
        private String collectionName;
    
        private boolean deleteAfterTest = false;
    
        @Before
        public void setUp() {
            this.collectionName = "test_search_by_date_" + +System.currentTimeMillis();
        }
    
        @After
        public void deleteCollection() {
            if (deleteAfterTest) {
                this.mongoTemplate.dropCollection(this.collectionName);
            }
        }
    
        public class TestClass {
    
            @Transient
            private DateTime myDate;
    
            @PersistenceConstructor
            private TestClass() {
            }
    
            public TestClass(DateTime date) {
                this.myDate = date;
    
            }
    
            public DateTime getMyDate() {
                return myDate;
            }
    
            public void setMyDate(DateTime myDate) {
                this.myDate = myDate;
    
            }
        }
    
        @Test
        public void should_create_iso_date_format() {
            DateTime dateTime = new DateTime(2011, 3, 3, 12, 0, 0, 0);
            DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
            String str = fmt.print(dateTime);
            assertThat(str, Matchers.is("ISODate(\"2011-03-03T11:00:00Z\")"));
        }
    
        @Test
        public void should_search_by_date() {
            DateTime dateTime = new DateTime(2011, 3, 3, 12, 0, 0, 0);
            TestClass testClass = new TestClass(dateTime);
            this.mongoTemplate.save(testClass, this.collectionName);
            List<TestClass> testClassList = this.mongoTemplate.find(
                    new Query(Criteria.where("myDateValue").is(ISODateTimeFormat.dateTime().print(dateTime))),
                    TestClass.class, this.collectionName);
            assertThat(testClassList.size(), is(1));
        }
    
        @Test
        public void should_search_by_date_as_date() {
            DateTime dateTime = new DateTime(2011, 3, 3, 12, 0, 0, 0);
            TestClass testClass = new TestClass(dateTime);
            this.mongoTemplate.save(testClass, this.collectionName);
            List<TestClass> testClassList = this.mongoTemplate.find(new Query(Criteria.where("myDate").is(dateTime)),
                    TestClass.class, this.collectionName);
            assertThat(testClassList.size(), is(1));
        }
    }

    Comment


    • #3
      Hi,

      Appreciate if some one can respond to my query above. Please let me know in case any you need any other details ? Thanks...

      Regards,
      Antony.

      Comment


      • #4
        It seems you were going a bit too manual when handling properties and fields. I've added a test case to the tests for MongoTemplate following your example [0]. The crucial part is to register custom Converters for DateTime to Date and vice versa and using Date as query parameter. Feel free to repost here if you have any further questions.

        [0] https://github.com/SpringSource/spri...4d0670c166e4b3

        Comment


        • #5
          Thanks a lot Oliver. Will try this and will get back to you in case we face any issues. Thanks again !!

          Regards,
          Antony

          Comment

          Working...
          X