how to write search query for date in MongoDb.

21,834 views
Skip to first unread message

nagamuni reddy

unread,
Dec 15, 2011, 12:20:55 PM12/15/11
to mongodb-user
how to write search query for date.
i am getting date dynamically from client side.
i need to get result from Db for that perticular date.
i searched in net they gave like ,

var start = new Date(2010, 3, 1);
var end = new Date(2010, 4, 1);

db.posts.find({created_on: {$gte: start, $lt: end}});


but i don't want to use two dates.
i need to use single date for search
please send response as soon as possible
it is too urgent.

like select * from emp table where date="12/14/2011" like that(in sql)

Sam Millman

unread,
Dec 15, 2011, 12:26:07 PM12/15/11
to mongod...@googlegroups.com
var date = new Date(2010,3,1);

db.posts.find({created_on: date});


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Robert Stam

unread,
Dec 15, 2011, 12:28:58 PM12/15/11
to mongod...@googlegroups.com
I personally prefer to use the ISODate constructor because it doesn't
have Javascript's Date's weirdness of months going from 0 to 11
(instead of 1 to 12).

var date = ISODate("2010-04-01")

nagamuni reddy

unread,
Dec 15, 2011, 8:52:04 PM12/15/11
to mongod...@googlegroups.com
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
  Calendar currentDate= Calendar.getInstance();
  Date dateNow = currentDate.getTime();
  JSONObject jsobj= new JSONObject();
  jsobj.put("todaydate", todaydate);
 mongoDb.append(query,jsobj );

data inside Db is 

   
{
    "phonehome" : "12412442",
    "adddate" : new Date("Mon, 12 Dec 2011 12:35:00 GMT +05:30")
}
i tried this query

db.users.find({ "adddate" : "new Date(2011,12,12)" });

but this is not working.







Below is the query i used:

Query                 :db.coll.find({},{"doctor":1});

Output retrieve    : {"doctor":{"visited":{"date":3,"day":6,"hours":12,"minutes":12,"month":11,"seconds":42,"time":1322894562328,"timezon
                              Offset":-330,"year":111}}}

I hope this will be clear.

nagamuni reddy

unread,
Dec 15, 2011, 8:53:58 PM12/15/11
to mongod...@googlegroups.com

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
  Calendar currentDate= Calendar.getInstance();
  Date dateNow = currentDate.getTime();
  JSONObject jsobj= new JSONObject();
  jsobj.put("todaydate", todaydate);
 mongoDb.append(query,jsobj );

data inside Db is 

   
{
    "phonehome" : "12412442",
    "adddate" : new Date("Mon, 12 Dec 2011 12:35:00 GMT +05:30")
}
i tried this query

db.users.find({ "adddate" : "new Date(2011,12,12)" });

but this is not working.



Robert Stam

unread,
Dec 15, 2011, 9:01:30 PM12/15/11
to mongod...@googlegroups.com
Two comments:

1. The date value stored in your database has a time component so it
can't match a date where the time component is zero
2. "new Date(2011, 12, 12)" is a string, not a Date, because you put
quotes around it

Also, 12 is not a valid month for the Javascript Date constructor
(which uses 0 - 11).

Not sure what the bit about the document with the doctor field was about.

nagamuni reddy

unread,
Dec 17, 2011, 1:05:12 AM12/17/11
to mongod...@googlegroups.com
db.users.find({ "adddate" : new Date(2011,10,11) });

if i write query by removing  quotes it is giving syntax error.

nagamuni reddy

unread,
Dec 17, 2011, 1:26:12 AM12/17/11
to mongod...@googlegroups.com
and how to write query for date in MongoVUE  tool.
if any one having document related to query on dates pls  send to me.

Robert Stam

unread,
Dec 17, 2011, 1:56:29 AM12/17/11
to mongod...@googlegroups.com
I tried your date query and I didn't get a syntax error:

> db.users.remove()
> db.users.insert({_id:1, adddate:new Date(2011,10,11)})


> db.users.find({ "adddate" : new Date(2011,10,11) });

{ "_id" : 1, "adddate" : ISODate("2011-11-11T05:00:00Z") }
>

I'm not very familiar with MongoVUE, but I would imagine it takes the
same queries as the mongo shell.

On Sat, Dec 17, 2011 at 1:26 AM, nagamuni reddy

nagamuni reddy

unread,
Dec 19, 2011, 1:05:31 PM12/19/11
to mongod...@googlegroups.com
thanks robert  
it is working fine in MongoVUE if i insert and search like what u mentioned.
in java if i write code 
Date adddate=new Date(2011,10,11);
i am not able to get result for this query
 db.users.find({ "adddate" : new Date(2011,10,11) }); (no syntax error and no output)

and my doubt is in java  new Date(2011,10,11)  is deprecated .so we can't pass adddate values   like that. 
so  can u suggest me how to write code for that date one in java.

Robert Stam

unread,
Dec 19, 2011, 1:27:25 PM12/19/11
to mongod...@googlegroups.com
Can you show us a sample document that you think should be returned but is not?

My first guess is that your Dates are not equal. Might be something as
simple as timezone issues.

I recommend you use the mongo shell ISODate helper function instead of
the Date constructor.

Here's a working example in Javascript in the mongo shell:

> db.test.remove()
> db.test.insert({_id:1,adddate:ISODate("2011-12-01")})
> db.test.insert({_id:2,adddate:ISODate("2011-12-02")})
> db.test.find()
{ "_id" : 1, "adddate" : ISODate("2011-12-01T00:00:00Z") }
{ "_id" : 2, "adddate" : ISODate("2011-12-02T00:00:00Z") }
> db.test.find({adddate:ISODate("2011-12-01")})
{ "_id" : 1, "adddate" : ISODate("2011-12-01T00:00:00Z") }
>

On Mon, Dec 19, 2011 at 1:05 PM, nagamuni reddy

Reply all
Reply to author
Forward
0 new messages