Date from MongoDB prints as "date":"2011-05-12T13:51:33Z" - json

Why am I getting the "T" and "Z" in the date when I fetch it from MongoDB and convert it to JSON using Rails3?
"date":"2011-05-12T13:51:33Z"
Thanks
Fetch:
#bs = coll.find("headers.from" => email, "date" => {"$gte" => initial_date, "$lte" => Time.now.utc})
Insert:
date : { type: Date, default: Date.now }

It's an ISO8601 formatted datetime. The 'T' separates the date from the time and the 'Z' indicates that the date is UTC (GMT). MongoDB doesn't support a Date (only) type, instead everything is converted to a timestamp.
You can drop into the mongo console and run a query you'll see date (and time) fields are stored as ISODate("2011-05-12T13:51:33Z").

Related

Logic App - Convert "YYYY-MM-DD HH:MM:SS" to Milliseconds since Epoch

I GET below JSON data from source with date in "YYYY-MM-DD HH:MM:SS" format, and I want to convert the it to the Milliseconds to Epoch before sending it to destination, can someone help how to do it in Logic App?
Source data:
{
"result":[
{
"number":"123",
"name":"ABC",
"created":"2018-09-19 09:03:03"
}
]
}
Desired data:
{
"result":[
{
"number":"123",
"name":"ABC",
"created":"1537304583000"
}
]
}
Azure logic app only has the ticks method that can convert the timestamp to an ticks number.
You can use the sub method to convert the value after ticks conversion to an Epoch by subtraction.
Expression:
sub(ticks('2018-09-19 09:03:03'),636727908525417000)
636727908525417000 is the difference between ticks('2018-09-19 09:03:03') and the 1537304583000 you gave.
You can refer to this article:
https://devkimchi.com/2018/11/04/converting-tick-or-epoch-to-timestamp-in-logic-app/
Logic Apps expression to get current time in Epoch format:
div(sub(ticks(utcNow()),621355968000000000),10000000)
Explanation:
utcNow() gets datetime, by default in format of "o" (yyyy-MM-ddTHH:mm:ss.fffffffK).
utcNow() can be replace with ticks("myTimestamp") or parseDateTime("myTimestamp") or trigger().startTime
ticks()/ticks(utcNow()) coverts datetime to 100-nanosecond intervals since January 1, 0001 12:00:00, aka "ticks".
Hardcoded value: 621355968000000000 - equal to ticks value of 1970-01-01T00:00:00Z, aka start of Unix Epoch.
sub()/sub(ticks(utcNow()),621355968000000000) subtracts number of ticks of your datetime from hardcoded ticks value of start of Unix Epoch.
div()/div(sub(ticks(utcNow()),621355968000000000),10000000) by dividing ticks by 10,000,000 you convert the ticks from 100-nanoseconds into milliseconds.

PyMongo - Delete documents based on a date condition where date is stored in string format (%m/%d/%y) in MongoDB

How to delete Mongodb documents based on certain date condition using PyMongo which stores the date object in string format in MongoDB ?
Sample code:
API takes a date parameter as payload and returns only that much data.So before inserting new documents, want to ensure previous data is not delete and only the ones which are being currently processed.
import requests
import json
from pymongo import MongoClient
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
past_4_month= datetime.today() + relativedelta(months=-4)
past_4_month_1st_day=past_4_month.strftime("%m/01/%Y")
url='xxx'
header={'Auth':'xyz', 'Content-Type': 'application/json'}
payload={'start_date': past_4_month_1st_day }
api_data = requests.request("POST", url, data=json.dumps(payload), headers=header).json()
client = MongoClient('mongodb://localhost:27017')
db = client[test_db][test_collection]
db.delete_many({'date':{'$gte':past_4_month_1st_day}})
db.insert_many(api_data)
client.close()
Sample output stored in mongodb:
{
"id": ObjectId("5f44e33ffb3be4ed19a10ed3"),
"xyz_id" : "12345",
"name" : "xyz",
"date" : "3/3/2020"
}
For example - when running in Aug-2020 - it will return data from past 4 months i.e. May-2020 onwards and always truncate/delete and load same data until end of Aug-2020.
On Sep-2020 1st of the month, the prev 4 month date would be June-2020, so it should not delete May-2020 data but only from June-2020 till current month (Sep-2020). Meaning only process past 4 months data always with every run (truncate/delete and reload).
However, since the date is stored as string in MongoDB document, tried using converting it to date from string using $dateFromString, $toDate and in Python etc. it is still not working as expected and deletes all the documents (from May-2020) from previous runs/loads.
Please suggest.

How to update a document in MongoDb with the field value itself in iterations?

I have a MongoDb document such as
{
"_id" : ObjectId("5731c75196dada69cd52419d"),
"businessName" : "Test",
"createdDate" : "2015-10-04 13:48:55",
"modifiedDate" : "2016-03-03 10:37:48"
}
If you notice the createdDate and modifiedDate column are in YYYY-mm-dd h:i:s format (Which is coming from my Mysql Db). I need to convert the dates into ISO format.
For this, I am using the below query .
db.users.update({"_id" : ObjectId("5731c75196dada69cd52419d")},
{$set : {"createdDate" : ISODate('2015-10-04 13:48:55')}} )
I am getting the desired result.
Now my question is if we are having lot of documents and need to update the field value then how can we do it in iterations in Mongodb itself.
Is that possible ? Updating a single document each time will be very time-consuming. Any help will be appreciated.
Firstly get created at values then convert it into ISODate store it your array and then pass that array to createdDate in $in operator.
You can use toISOString() of javascript to convert values.
db.getCollection('users').update({'_id':{$in:[ObjectId('574d61acf834e66681d37804'),
ObjectId('574d6185f834e66681d37803')]}},
{$set : {"createdDate" :{$in:['your converted date 1','your converted date 2']}},
{multi: true})

Laravel format DateTime from database result

My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS and display it on my view in a table cell. My database dates are called date_begin and date_end.
Better, when I get this dates from database, convert it to DD.MM.YYYY format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.
How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.
Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problem whatsoever.
If you're using Eloquent Models you can do it very easily:
1. Add the columns to the $dates property in your model class:
protected $dates = ['date_begin', 'date_end'];
This will ensure that the values get mutated to Carbon instances.
2. In your view files you can use the format method that Carbon offers like so:
<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}
<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}
<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}
There's no need to split the value in time and date, just show what you want from the DateTime value using whatever format you want.
If you're not using Eloquent models, then you can manually use Carbon to format your value like so:
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
I have used the PHP solution without Carbon:
Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin
to:
date('d-m-Y H:i:s', strtotime($item->date_begin))
After that, when you save it on the database add on the DB update:
date('Y-m-d H:i:s', strtotime($request->date_begin))
protected $casts = [
'inicio' => 'datetime:Y-m-d\TH:i'
,'entrega' => 'datetime:Y-m-d\TH:i'
];
you could use it for the datetime-local field. greed you. like me if work for you
// attributes that should be cast to native types
protected $casts = [
'email_verified_at' => 'datetime'
];
// get formatted datetime string for email_verified_at
public function getEmailVerifiedAttribute()
{
if ($this->email_verified_at) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $this->email_verified_at, 'UTC');
return $date->setTimezone($this->timezone->name)->isoFormat('LLLL');
} else {
return null;
}
}
as Laravel document way.

ReactiveMongo & Play: How to compare two DateTime instances

I use Play-ReactiveMongo to interact with MongoDB... and I'm wondering how to compare two dates considering that I don't use BSON in my application. Let me provide you with an example:
def isTokenExpired(tokenId: String): Future[Boolean] = {
var query = collection.genericQueryBuilder.query(
Json.obj(
"_id" -> Json.obj("$oid" -> tokenId),
"expirationTime" -> Json.obj("$lte" -> DateTime.now(DateTimeZone.UTC))
)
).options(QueryOpts(skipN = 0))
query.cursor[JsValue].collect[Vector](1).map {
case Some(_) => true
case _ => false
}
}
isTokenExpired does not work as expected since expirationTime is considered a String – I've an implicit Writes that serializes a DateTime as "yyyy-MM-ddTHH:mm:ss.SSSZ"... and this is correct since I want a human-readable JSON.
That said, how do I get a document from a collection that has a DateTime less than another DateTime? The following doesn't seem to work:
Json.obj(
"_id" -> Json.obj("$oid" -> tokenId),
"expirationTime" -> Json.obj("$lte" -> Json.obj("$date" -> DateTime.now(DateTimeZone.UTC).getMillis))
)
Thanks.
I've an implicit Writes that serializes a DateTime as "yyyy-MM-ddTHH:mm:ss.SSSZ"... and this is correct since I want a human-readable JSON.
If you store your DateTime as a string in MongoDB then $lte won't compare the dates.
You should store your DateTime as as date in MongoDB (with $date) so you can use your second query (the one with $lte and $date).
I want a human-readable JSON
Why do you need human-readable JSON? I don't see any reason against the date datatype (If you need human-readable JSON in your API then convert your date field there).
The MongoDB dates are readable. Output in MongoDB shell:
PRIMARY> db.mycollection.findOne()
{
"creation" : ISODate("2014-01-16T14:45:27.441Z")
}