how to produce ISODate type from javascript ?

7,031 views
Skip to first unread message

life0fun

unread,
Nov 7, 2011, 12:46:39 AM11/7/11
to mongodb-user
Hi,
I have a timestamp field with isodate type :
ISODate("2011-08-14T19:44:15Z").
When I query from mongo shell, it works if I use new Date(),
$lt:new Date(1314547200000)
However, if I am using int number, i.e., $lt: 1314547200000. it
fails.

new Date() in mongoshell produces ISODate(), that why it works.

I am using mongo-node-native, new Date() in javascript gives me JS
date object. How can I pass ISODate type to mongo from javascript ?
The cook book gives an example with python using datetime object.
I am using mongo-node-native.

Sam Millman

unread,
Nov 7, 2011, 3:13:50 AM11/7/11
to mongod...@googlegroups.com
Prolly because that isn't really a valid timestamp.

Using a converter I got more like:

1313351055

For your date time.

This might not be correct since it is only a rough estimation but still the time number you have provided is much much greater than a timestamp.


--
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.


life0fun

unread,
Nov 7, 2011, 11:02:15 AM11/7/11
to mongodb-user
The number I am give is epoch in milliseconds.
Mongodb isodate is int64 epoch time precision to milliseconds.

when I use it in mongoshell, it works fine.
tm: { $gt:new Date(1314536400000), $lt:new Date(1314547200000)} })

In my javascript, None of the following works
whereobj.tm=1314547200000
whereobj.tm=new Date(1314547200000)
whereobj.tm='ISODate("2011-08-28T15:49:35Z")'

Wondering how to query date field in mongo from nodejs.

On Nov 7, 2:13 am, Sam Millman <sam.mill...@gmail.com> wrote:
> Prolly because that isn't really a valid timestamp.
>
> Using a converter I got more like:
>
> 1313351055
>
> For your date time.
>
> This might not be correct since it is only a rough estimation but still the
> time number you have provided is much much greater than a timestamp.
>

Antoine Girbal

unread,
Nov 8, 2011, 12:27:34 AM11/8/11
to mongodb-user
using "new Date(1314536400000)" should work both in shell and node.js.
are you sure that you connect to the right db/collection? (maybe you
can test by printing the result of collection.count())

life0fun

unread,
Nov 8, 2011, 3:57:23 PM11/8/11
to mongodb-user
This is the log from mongo with
whereobj.tm=new Date(1314547200000)

tm: { $gt: "2011-08-28T13:00:00.000Z", $lt:
"2011-08-28T16:00:00.000Z" }

new Date(1314547200000) in javascript will give a date object.
coffee> new Date(1314536400000)
Sun, 28 Aug 2011 13:00:00 GMT

new Date() in mongo shell work because mongo shell wrap it into
isodate() type. However, in node.js, it is not.

Date time should be the basic and it not working....not sure what's
wrong.

On Nov 7, 11:27 pm, Antoine Girbal <anto...@10gen.com> wrote:
> using "new Date(1314536400000)" should work both in shell and node.js.
> are you sure that you connect to the right db/collection? (maybe you
> can test by printing the result of collection.count())
>
> On Nov 7, 8:02 am, life0fun <life0...@gmail.com> wrote:
>
>
>
>
>
>
>
> > The number I am give is epoch in milliseconds.
> > Mongodbisodateis int64 epoch time precision to milliseconds.
>
> > when I use it in mongoshell, it works fine.
> >    tm: { $gt:new Date(1314536400000), $lt:new Date(1314547200000)} })
>
> > In myjavascript, None of the following works
> >     whereobj.tm=1314547200000
> >     whereobj.tm=new Date(1314547200000)
> >     whereobj.tm='ISODate("2011-08-28T15:49:35Z")'
>
> > Wondering how to query date field in mongo from nodejs.
>
> > On Nov 7, 2:13 am, Sam Millman <sam.mill...@gmail.com> wrote:
>
> > > Prolly because that isn't really a valid timestamp.
>
> > > Using a converter I got more like:
>
> > > 1313351055
>
> > > For your date time.
>
> > > This might not be correct since it is only a rough estimation but still the
> > > time number you have provided is much much greater than a timestamp.
>
> > > On 7 November 2011 05:46, life0fun <life0...@gmail.com> wrote:
>
> > > > Hi,
> > > >   I have a timestamp field withisodatetype :
> > > >ISODate("2011-08-14T19:44:15Z").
> > > >   When I query from mongo shell, it works if I use new Date(),
> > > >       $lt:new Date(1314547200000)
> > > >    However, if I am using int number, i.e., $lt: 1314547200000. it
> > > > fails.
>
> > > >    new Date() in mongoshell producesISODate(), that why it works.
>
> > > >    I am using mongo-node-native, new Date() injavascriptgives me JS

Sam Millman

unread,
Nov 8, 2011, 4:15:52 PM11/8/11
to mongod...@googlegroups.com
Which driver are you using from client side is it one of the ones for node.js or are you connecting some other way?

This will help us narrow down why it aint working.

life0fun

unread,
Nov 9, 2011, 1:21:58 AM11/9/11
to mongodb-user
Yeah....I am also thinking there are some data type serialization
issue with the driver.
I am using node-mongodb-native installed by
npm install mongodb

I am using node.js and just could not find an example of date time for
node.js. I thought date time is such a basic thing you sure need to
provide good api doc/example for it.....sadly I could not find any
from both the driver and mongo side.

Thanks,

On Nov 8, 3:15 pm, Sam Millman <sam.mill...@gmail.com> wrote:
> Which driver are you using from client side is it one of the ones for
> node.js or are you connecting some other way?
>
> This will help us narrow down why it aint working.
>
Reply all
Reply to author
Forward
0 new messages