current time?

7,119 views
Skip to first unread message

wul...@gmail.com

unread,
Feb 14, 2011, 7:56:10 PM2/14/11
to mongodb-user
how do i get the current time in mongo?

in mysql the equiv is:
select now();

Scott Hernandez

unread,
Feb 14, 2011, 8:03:09 PM2/14/11
to mongod...@googlegroups.com
You can use db.eval("return new Date()"). Why do you want the server date/time?

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

sridhar

unread,
Feb 14, 2011, 8:06:16 PM2/14/11
to mongodb-user

Alvin Richards

unread,
Feb 14, 2011, 8:06:18 PM2/14/11
to mongodb-user
db.eval( function() { return new Date() } );

-Alvin

Stephen Steneker

unread,
Jul 3, 2012, 2:45:15 AM7/3/12
to mongod...@googlegroups.com
Can you tell me how to use Java to get the current time?  Thank you very much.

Hi,

There is no function to get the server time in the current production version of MongoDB.

The 2.1.1 unstable/development release added 'localtime' to db.isMaster() so when 2.2 is released you will be able to get this via the equivalent of:
   db.isMaster().localTime

Cheers,
Stephen

Russell Bateman

unread,
Mar 6, 2013, 9:45:54 AM3/6/13
to mongod...@googlegroups.com
There's also

    long millis = System.currentTimeMillis();

--since you just seem to want it in milliseconds. This has the advantage of by-passing construction of a Date.


On 3/6/2013 5:15 AM, w.fi...@gmail.com wrote:


W dniu wtorek, 3 lipca 2012 08:08:52 UTC+2 użytkownik 张铁军 napisał:
Can you tell me how to use Java to get the current time?  Thank you very much.

new Date().getTime()
- now works in mongo db, for example:
db.my_collection.find({date: {$gt: (new Date().getTime() - someTimeInMiliseconds}})


在 2011年2月15日星期二UTC+8上午9时06分18秒,Alvin Richards写道:
--
--
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
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Achille MBOUGUENG

unread,
Mar 6, 2013, 7:53:59 AM3/6/13
to mongod...@googlegroups.com
Hi  how  to  subscribe here ?? Thanks


2013/3/6 <w.fi...@gmail.com>


W dniu wtorek, 3 lipca 2012 08:08:52 UTC+2 użytkownik 张铁军 napisał:
Can you tell me how to use Java to get the current time?  Thank you very much.

new Date().getTime()
- now works in mongo db, for example:
db.my_collection.find({date: {$gt: (new Date().getTime() - someTimeInMiliseconds}})


在 2011年2月15日星期二UTC+8上午9时06分18秒,Alvin Richards写道:
db.eval( function() { return new 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
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Achille MBOUGUENG
Tel: 0033(0)761705745
skype: sim_patricx
web: achillesimo.creativdev-mada.com
         www.andandoo.info


Santiago Ezcurra

unread,
Sep 5, 2013, 10:03:41 AM9/5/13
to mongod...@googlegroups.com
Hey Scott. this seems a good alternative....i cannot figure out how to write this as part of an insert from a client machine (e.g. from a .Net application, using the c# mongodb driver).

For example, my reason to include server time is to be able to filter later by that field, for "documents older than server time - 10 minutes"
Right now I'm just sending the value of .Net Datetime.Now() for my datetime field, but the problem I'm having with this option is that if the application server time gets out of sync with mongodb server, then i would insert documents with different time than mongodb server, and subsequent queries (for example, a query that deletes documents older than 10 minutes) would throw wrong results.

Asya Kamsky

unread,
Sep 5, 2013, 8:27:05 PM9/5/13
to mongodb-user
You might keep an eye on this ticket then: https://jira.mongodb.org/browse/SERVER-10123

That describes a mechanism that would allow you to use server time for updates - I'm not sure if a similar mechanism can be implemented for queries...

Asya




--
--
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
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Santiago Ezcurra

unread,
Sep 6, 2013, 9:39:30 AM9/6/13
to mongod...@googlegroups.com
Well, this could work...but it's still not implemented, right ?

S.
Reply all
Reply to author
Forward
0 new messages