How Spring Data Mongodb convert date from $date to ISODate - mongodb

Spring Data Mongodb auto parser date object to '$date': millisecond, this query consume more than 1 min to return data, I find ISODate query is more quickly than $date, How I make Spring Data Mongodb parser date object to ISODate.

It is base on your DataType, As i use Instant, mongodb auto convert it to ISODate.
But as you said the date type as millisecond, that cost many time to return data, you should check your query first....
Please show the Object and query for more support

Related

Is there a way we can get date alone from a datetime field stored as string in MongoDB. We are using Spring MongoDB driver

Let's say I have an date attribute stored in the format dd/MM/YYYY HH:mm:ss i.e. 22/12/2021 10:15:23. Now I just want 22/12/2021 as a date value so that I can do date comparisons. I want this operation to happen at database itself so that I can do date comparisons as part of the query/aggregation itself. Please note I am using Spring MongoDB driver.
Getting date alone from a datetime string is not possible in MongoDB since it always returns an ISO date with time. As a workaround we can convert the time HH:mm:ss to 00:00:00 either by applying String operations on the string attribute or by using Arithmetic operations on the date. We will have to add a temporary field in the query which will hold the converted value using add field operations. And then we can compare them with a given date.

Convert date math expression to date Elasticsearch

I'm looking for a nice way to retrieve date and time information from a date math expression passed to Elasticsearch. If I query on a time range using e.g 'now-2d/s' as gt-value, is there any way to know what date was actually used internally by Elasticsearch?

Store/Index time in mongo, use a timestamp or a date/time

When storing a timestamp in mongodb should I store the 'unix-time' millis since some date or should I store an actual date/time?
What are the benefits of either?
Edit
To be more specific should I store a long that is seconds since Jan 1. 1970, or should I store a Javascript date object.
I recommend using the MongoDB native date format. The advantage of that is that you can then easily use the date operators:
http://docs.mongodb.org/manual/reference/operator/aggregation-date/

Nanoseconds lost coming from MongoDB ISODate Object

I'm losing the nanoseconds from the MongoDb interface for the ISODate object. All the nanoseconds are set to zero when I read them in perl.
First, my environment:
MongoDB version: 1.8.2
perl v5.12.4
MongoDB perl module version: 0.701.4
I have a Mongo DB that has rtcTime coded as an ISODate, as follows:
"rtcTime" : ISODate("2013-05-13T18:54:55.918Z")
The code to extract the rtcTime looks something like this:
my #results = $db->get_collection( 'timings' )->find( )->all();
foreach my $record ( #results )
{
print $record->{rtcTime}->nanoseconds()."\n";
}
Output is all 0's.
To fully reproduce the problem, create an ISODate object with arbitrary (non-zero) hires_epoch values in the MongoDB database. Then try to use the MongoDB / DateTime / DateTime::Format::ISO8061 modules to extract any kind of hires time data.
Q: Why can't I get my milliseconds, microseconds, or nanoseconds from the MongoDB ISODate data?
MongoDB stores documents in BSON format and its specification says:
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970).
So, date precision is limited to miliseconds. If you really need to store nanoseconds, you shouldn't use date type. You have to use a long and store the timestamp, so that you'll no lose precision.
This is a bug in the way that the MongoDB Perl driver interacts with DateTime. When ISODate values are retrieved from the database, the driver initializes the DateTime object using its from_epoch constructor. But the nanosecond portion is not passed to the constructor.
DateTime does support passing the full value including nanoseconds, and the driver should be updated to fix that.
I've created a ticket for this bug and I will fix it. But maybe not until after the holiday weekend. :)

How to convert date format in mongodb

I am using an API (so I have no control over the fields) - one of the fields is of type Date.
Now, I find that the date stored in the records resembles the following value:
{
...
"foobarDate": "2013-05-26T05:00:00.000Z",
...
}
Now I wish to retrieve some records based on this date - for example, I need to retrieve all the records whose fooBarDate is earlier than today's date.
If I use Javascript's Date() functionality, then I get the date in the following format:
Wed Jun 19 2013 21:13:50 GMT+0530 (IST)
If I try to design my query as follows...
{
"fooBarDate": {
"$lte": <the date calculated in javascript>
}
}
...I do not get the records - I get an empty array - I suspect it is due the reason that the format of the Date stored in MongoDB is different from the one I am passing.
How to convert the Javascript date format to the mongodb format? Or at least, how do I get the current date in the format stored in MongoDB?
P.S. I am using nodeJs and wish to query the DB in this case.
Mongo stores dates as ISODate objects. Just wrap our date into ISODate constructor:
{
"fooBarDate": {
"$lte": ISODate("2013-05-26T05:00:00.000Z")
}
}
The correct data format if you pass a string into the query is ISO8601 which is looks like you have done, however you can also pass the javascript date object into the query directly and get the same result. It looks like you may be missing the proper brackets which could be causing your query to fail. Try reformatting your code in this manner (edit I am using mongoose, but I think straight mongo is the same):
var currentTime = new Date;
orm.Records.find({lastUpdate:{$lte:currentTime}})