MongoDB: Convert Date String (mm/dd/yyyy) to Unix timestamp - mongodb

just practicing my MongoDB queries and I've hit a wall with a field data type.
I'm currently using Robomongo as GUI for accessing the production database.
My document structure looks like:
Is there a MongoDB operator or way/method to convert the date field value, currently in mm/dd/yyyy format, to a Unix timestamp so we can perform filter operations?

You can iterate all your items and update one by one with the conversion to Date. Here is an example to convert your date from mm/dd/yyyy to ISODate :
db.test.find().forEach( function(res){
if (typeof(res.date)=="string"){
var arr = res.date.split("/");
res.date = new Date(arr[2], arr[0] - 1, arr[1]);
db.test.save(res)
}
}
)
For Unix timestamp (millis from epoch), you can call getTime() from Date :
db.test.find().forEach( function(res){
if (typeof(res.date)=="string"){
var arr = res.date.split("/");
res.date = new Date(arr[2], arr[0] - 1, arr[1]).getTime();
db.test.save(res)
}
}
)
Note that these dates will be converted into UTC format, so you may want to change temporarily your timezone before doing your conversion
You can also use bulk update if you want to optimize update performance
You can also just convert your date to yyyy-mm-dd which will preserve sorting (check this post). The following will decompose your date field into day,month and year, set date field with the new format and write output in a new collection named test2 :
db.test.aggregate([{
$project: {
startTime: 1,
endTime: 1,
date: {
$let: {
vars: {
year: { $substr: ["$date", 6, 10] },
month: { $substr: ["$date", 0, 2] },
dayOfMonth: { $substr: ["$date", 3, 2] }
},
in : { $concat: ["$$year", "-", "$$month", "-", "$$dayOfMonth"] }
}
}
}
},{
$out :"test2"
}])

Related

mongodb: Zulu dates

Question is straightforward.
It's not clear to me if mongodb is ALWAYS storing dates in UTC, so in zulu(Z) zone, regardless if date string contains a zone?
I've tested with:
> db.products.updateOne( { _id: 1 }, { $set: { item: "apple" }, $setOnInsert: { dateAdded: new Date() } }, { upsert: true });
> db.products.updateOne( { _id: 2 }, { $set: { item: "jordi" }, $setOnInsert: { dateAdded: new Date("1982-05-19T14:00:00.000+05:00") } }, { upsert: true });
I detected that second date is stored a Zulu(Z):
db.products.find();
[
{
_id: 1,
dateAdded: ISODate("2022-02-02T15:40:02.457Z"),
item: 'apple'
},
{
_id: 2,
dateAdded: ISODate("1982-05-19T09:00:00.000Z"),
item: 'jordi'
}
]
Related question is, how do I need to make date range queries?
I mean, ranged dates queries have to have dates using Zulu zone?
I've tested a bit. It seems I'm able to set range queries using whichever timezone and they are transalted to Zulu:
db.products.find({ dateAdded: { $gt: ISODate("1982-05-19T13:00:00.000+05:00"), $lt: ISODate("1982-05-20T00:00:00.000Z") } });
[
{
_id: 2,
dateAdded: ISODate("1982-05-19T09:00:00.000Z"),
item: 'jordi'
}
]
The internal representation of a date doesn't refer to UTC or any other time zone, but represents a specific instant in the history of the world. Specifically, the MongoDB manual says:
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
So, a date and time of "1969-12-31T16:00:00 America/Los_Angeles" or 1970-01-01T03:00:00 Africa/Nairobi" would both be stored as the number zero, because they correspond to the arbitrary "epoch", chosen to fall at "1970-01-01T00:00 UTC".
For input, dates constructed from any of those date strings would result in the same internal value, so compare as equal.
For output, you can choose the timezone to display (e.g. which of the three strings above you want to show for an internal value of zero) by using the timezone argument to $dateToString. If you don't specify it, UTC will be used as a default, but that doesn't reflect the internal storage, just an arbitrary default for that parameter.

MongoDB query based on date plus time

i've to run a query like this (sql) in MongoDb 4:
SELECT * FROM log WHERE DATE_ADD(created_at, INTERVAL 2 HOUR) < NOW()
Basically, I want to find all the documents, in the PENDING state, whose creation date PLUS TWO HOURS is less than now .. Let me explain: I want to find all the documents in the PENDING state that have been in PENDING for more than two hours.
I feel stupid, but I am failing to do this with MongoDb.
I also created a playground:
https://mongoplayground.net/p/4bifqiX2KMJ
Can you help me?
You can add hours in ISO date using $add, convert string date to ISO date using dateFromString,
let date = new Date();
db.collection.find({
status: "pending",
$expr: {
$lt: [
{
$add: [
// convert string date to ISOdate, if its already then use only "$inserted_at"
{ $dateFromString: { dateString: "$inserted_at" } },
// add milliseconds
7200000 // (60*60*2000)
]
},
date
]
}
})
Playground
Or subtract from current date and then compare the condition,
let date = new Date();
date = new Date(date.getHours()-2); //subtract 2 hours
db.collection.find({
status: "pending",
$expr: {
$lt: [
{ $dateFromString: { dateString: "$inserted_at" } },
date
]
}
})
Playground

MongoDB: how to parse date in 3.6 mongoDb version?

I have created Mongo Playground here
Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field)
Currently $dateToString and $dateFromString is using format to parse the date.
I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString)
parsedDate: {
$dateFromString: {
dateString: "$_id.dateHour",
format: "%Y-%m-%dT%H"
}
}
If I remove format field from both $dateToString and $dateFromString, query still runs but output for "dateHour" shows different value than expected. (as below)
"dateHour": ISODate("2020-03-20T18:46:50Z"),
format field is not supported in 3.6. Have to make this query compatible for 3.6 version.
Final output has no change.
Main focus is to get "dateHour" value same after this change.
Current output :
"dateHour": ISODate("2020-03-19T18:30:00Z"),
expected output:
"dateHour": ISODate("2020-03-19T18:30:00Z"),
Use $dateToParts and its counterpart $dateFromParts
Here is an updated Playground
What it does is basically break the date into its parts:
{
$project: {
dateHour: {
$dateToParts: {
date: "$updatedAt"
}
}
}
}
would produce:
{
"dateHour": {
"day": 19,
"hour": 18,
"millisecond": 0,
"minute": 21,
"month": 3,
"second": 5,
"year": 2020
}
}
and then later you reconstruct the date from its parts:
{
$project: {
reconstructedDateHour: {
$dateFromParts: {
year: "$dateHour.year",
month: "$dateHour.month",
day: "$dateHour.day",
hour: "$dateHour.hour"
}
}
}
}

How to select/find data from inner array in MongoDB?

How I can extract (find) tags for date 2013-01-14?
db.test1.insert(
{
date: Date("2013-01-14"),
name: "Roma",
tags: [{Python:14,Ruby:10,C:4}]
}
)
I tried extract all info for current date, but even this request do not work:
db.test1.find({date:Date("2013-01-14")})
The mongo shell wrap objects of Date type with the ISODate helper but the objects remain of type Date. So when inserting dates in MongDB, you could use the
ISODate() constructor which returns a Date object using the ISODate() wrapper instead of the Date() method which returns the current date as a string.
When you query, use the new Date() constructor which returns a Date object using the ISODate() wrapper to get a date object that you can then use in the query, bearing in mind that JavaScript date objects months are zero-based index thus January has the value 0 in the constructor parameter.
Inserting:
db.test1.insert({
"date": ISODate("2013-01-14"),
"name": "Roma",
"tags": [
{ "Python": 14, "Ruby": 10, "C": 4 }
]
})
Querying:
var myDateObj = new Date(2013, 0, 14) // JavaScript Date object months are zero based index
db.test1.find({ "date": myDateObj }, {"_id": 0, "tags": 1})
Result:
/* 0 */
{
"tags" : [
{
"Python" : 14,
"Ruby" : 10,
"C" : 4
}
]
}

Timezone in mongo query

I have data inserted in UTC time format in mongodb. I want timings to be converted based on the timezone. Is there any possibility to do so in mongo query?
In mongo version 3.6 timezone has been added, mongo doc
expression to extract date part with timezone is
{ date: <dateExpression>, timezone: <tzExpression> }
we can either specify the timezone or offset while getting the date parts.
see my answer posted here
to get date from date with timezone America/Chicago
{ $month: {
date: new Date(),
timezone: "America/Chicago"
} }
or with offset
{ $month: {
date: ISODate(),
timezone: "-0500"
} }
Let consider your document contains ISODate as below :
db.collection.insert({"date":new Date()})
Above query insert date in ISODate format now you want to convert this ISODate into give timeZone.
Suppose you want to convert above date to Eastern Daylight Saving Time ( EDT ) epoch time zone conertor then offset converted as 14400 * 1000. First convert ISODate to timeStamp and then use substract EDT OffsetintimeStampand then converttimeStamptoISODate` again.
Check below aggregation query :
db.collection.aggregate({
"$project": {
"timestamp": { //convert ISODate tom timestamp
"$subtract": [{
"$divide": [{
"$subtract": ["$date", new Date("1970-01-01")]
}, 1000]
}, {
"$mod": [{
"$divide": [{
"$subtract": ["$date", new Date("1970-01-01")]
}, 1000]
}, 1]
}]
}
}
}, {
"$project": {
"timeZoneTimeStamp": {
"$subtract": [{ //substract timestamp to given offset if offset will in postive then replace subtract to add
"$multiply": ["$timestamp", 1000]
}, 14400000]
}
}
}, {
"$project": {
"timeZoneTimeStamp": 1, //converted timeZoneTimeStamp if required
"_id": 0,
"newDate": { // newDate is converted timezone ISODate
"$add": [new Date(0), "$timeZoneTimeStamp"]
}
}
})
NOTE :
In above query conversion from ISODATE to timeStamp ref. here
In case if the dates are not changed and constant e.g. something like created_record_date then whichever timezone data you need it, you should pre-calculate and save (as String) along with the same document so that you don't have to run the huge processing at the runtime which could slow down the execution time. in case you have existing records and you want to store the various different timezone data along with the records, think about running a Map-Reduct job and update the documents separately. (let me know if you need the code for that). However, if this date field can be changed as per the business logic then its wise to calculate at runtime. both techniques have their different use cases and their pros and cons.
-$
If you are using mongoose (probably also works in native driver):
import moment from 'moment-timezone'; // this is needed to use .tz() method
import mongoMoment from 'mongodb-moment';
// Initalize mongodb-moment so you can use moment() object directly in mongo query
mongoMoment(moment);
// Add timezone to your_date
const date = moment(your_date)
.tz("Europe/Zagreb");
// Make $gte/$lte queries with date ...