querying Date with mongoDB Java driver

474 views
Skip to first unread message

manu Manouch

unread,
Aug 20, 2012, 11:20:25 AM8/20/12
to mongod...@googlegroups.com
Hi all,

1) In a groovy script, I insert document from an Oracle database.

Oracle version of my data :

Origin Destination Day ...
Lille    Paris        2021-08-21

MongoDB version :
{Origin : Lille, Destination : Paris, Day : IsoDate("2012-08-21T22:00:00Z") }


2) in a second groovy script I try to know if a row in my Oracle database is present or not in my mongoDB database :

Statement = '''........'''

sql.eachRow(Statement)
{
    println(it.origin_city_code)
    println(it.destination_city_code)
    println(it.day)
   
    BasicDBObject query = new BasicDBObject();
    query.put("Origin", it.origin_city_code);
    query.put("Destination", it.destination_city_code);
    query.put("Day", it.day);

    nbrOffer = coll.getCount(query);
    println(nbrOffer)
}

For a document actually present in mongo, the querydoes not return me any value.
The date format is not the same I guess.

Any idea please ?

Jeff Yemin

unread,
Aug 20, 2012, 9:13:37 PM8/20/12
to mongod...@googlegroups.com
I confirmed that the driver works with java.sql.Date.  This code:

        DBCollection c = new Mongo().getDB("FREE15871").getCollection("test");

        java.sql.Date d = new Date(System.currentTimeMillis());

        c.insert(new BasicDBObject("d", d));
        System.out.println(c.findOne(new BasicDBObject("d", d)));

works as expected.  It finds the document that was just inserted.  Maybe a time zone mismatch?  Dates in MongoDB are in millisecond precision.


-jeff

manu Manouch

unread,
Aug 21, 2012, 5:30:59 AM8/21/12
to mongod...@googlegroups.com
my oracle query return me a set of row like this :

FRAVN    FRLIL    2012-11-15 12:00:00

I've to check if the current row is actually present in mongo.
If not, I've to insert it.

So, I did the following in Groovy :


BasicDBObject query = new BasicDBObject();
query.put("Origin", it.origin_city_code);
query.put("Destination", it.destination_city_code);
query.put("Day", it.day);
   
returnQuery = Agoffer.getCount(query)

But I have no result cause of the Date format (I'm sure that my data is actually in the data base)

If I remove the Day in my query like this :


BasicDBObject query = new BasicDBObject();
query.put("Origin", it.origin_city_code);
query.put("Destination", it.destination_city_code);
   
returnQuery = Agoffer.getCount(query)

println(returnQuery["Day"])

give me this :

Wed Jun 13 00:00:00 CEST 2012


My question is : How can I match the date between Oracle and mongoDB ?

Thank you :)







Jeff Yemin

unread,
Aug 21, 2012, 7:55:09 AM8/21/12
to mongod...@googlegroups.com
Have you confirmed that it.day.equals(returnQuery["Day"]) ?
Reply all
Reply to author
Forward
0 new messages