I have a query that works fine in mongodb compass, but the moment I bring it over into Jupyter it breaks. The issue is something to do with the date filter.
I have tried both:
cursor = prod_db.my_collection.find({"date": {"$eq": "new Date('2021-04-26')"}, "type": "Regular"})
as well as
cursor = prod_db.my_collection.find({"date": "new Date('2021-04-26')", "type": "Regular"})
If I remove the date query, I get a return that I would expect, which validates that the db connection is set up properly and that the "type" filter is valid. What am I missing here?
new Date is the date keyword for MongoShell.
You should replace it with python's built-in datetime package.
from datetime import datetime
cursor = prod_db.my_collection.find({"date": datetime(2021, 4, 26), "type": "Regular"})
Issues mapping date columns across python and Mongo happens primarily because Mongo stores the date as a JavaScript date object and then wraps the ISODate format around it. However if the date values that we are trying to pass from python onto mongo is in datetime format ( like #hhharsha36 mentioned above) one should be good
I found this article helpful when I was going through some hellish time using dates from pandas and python variables and using that to query/insert/update in Mongo : (https://medium.com/nerd-for-tech/how-to-prepare-a-python-date-object-to-be-inserted-into-mongodb-and-run-queries-by-dates-and-range-bc0da03ea0b2)
Related
In my PostgreSQL database, the datetime stored as 2022-05-10 10:44:19+08 and when I get
the datetime by using the sequelize, it will give in format:: 2022-05-10T02:44:19.000Z.
So, my question is how to convert to 2022-05-10 10:44:19 ?
Thanks in advance.
There is a direct dependence on the time of your server. Therefore, depending on what you want to get, you can use different options.
Here is a dbfiddle with examples
I am trying to read some data from the SharePoint API via the older _vti_bin/client.svc endpoint
I can't seem to find what type of date format this is and how I can parse it via C#.
The timestamp being returned is:
"LastContentModifiedDate": "/Date(2022,3,18,13,12,28,990)/"
The year and month are obvious so I could parse it myself if I knew what all the values are. Is there a formal definition for this or a way to parse this reliably? Is this a DateTime or DateTimeOffset or something else?
I just get an exception when trying to deserialize to a DateTime or DateTimeOffset.
The /Date(...)/ format is Microsoft's built-in JSON date format.
You can try to parse it using the code below.You can also check out this post, which provides a lot of methods.
using System.Web.Script.Serialization;
//code
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
DateTime ddate = json_serializer.Deserialize<DateTime>(#"""\/Date(1326038400000)\/""").ToLocalTime();
I am using pymongo 3.11.3 in my notebook project. The value for int32 is returning constant 100 by pymongo. Query is returning fine in MongoDB IDE. Here is the code
client = MongoClient("localhost", 27017)
db = client['mongodb_vs_mysql']
mongo_result = db.collection['covid19'].find().sort("Cases_person", -1).limit(30);
for i in list(mongo_result):
print(i)
The database has different values but when querying with pymongo, it is showing 100 for that column.
Need help
I'd be fairly certain that you're looking at different databases; for a start, you have different (albeit similar) ids, and different field names (Daily_Cumulative vs Daily / cumulative)
I was doing mistake in this code
db.collection['covid19'] should be db['covid19']
So I'm trying to convert a bunch of hours (10:00:00, 14:00:00, etc) from a given timezone to UTC.
When I do so, I keep maddeningly getting things back like "15:51:00".
When you get to that line, and print what value it's using, it's using something like:
1900-01-01 12:00:00-05:51
Which is fine, except for the -05:51 bit. I have no idea why that -05:51 is there, and it's causing me problems. UTC conversion is hour to hour, yes? I think it's got something to do with my timezone conversions, but I really don't get why they would be doing that.
Here's a minimal example that has the same erroneous output; it returns 15:51:00 when it should just return a flat hour, no minutes.
import datetime
from dateutil import tz
jj = datetime.datetime.strptime("10:00:00", "%H:%M:%S")
tzz = tz.gettz('US/Central')
def constructstring(tzz,x):
inter2 = x.replace(tzinfo=tzz) #ERROR HAPPENS HERE (I'm pretty sure anyways)
inter3 = inter2.astimezone(tz.tzutc())
return inter3
print(constructstring(tzz,jj).strftime("%H:%M:%S"))
You are not specifying a date when you create the jj datetime object, so the default date of 1900-01-01 is used. Timezones are not fixed entities; they change over time, and the US/Central timezone used a different offset back in 1900.
At the very least, use a recent date, like today for example:
# use today's date, with the time from jj, and a given timezone.
datetime.datetime.combine(datetime.date.today(), jj.time(), tzinfo=tzz)
If all you need is a time, then don't create datetime objects to store those; the datetime module has a dedicated time() object. I'd also not use strftime() to create objects from literals. Just use the constructor to pass in integers:
jj = datetime.time(10, 0, 0) # or just .time(10)
Other good rules of thumb: If you have to deal with dates with timezones, try to move those to datetime objects in UTC the moment your code receives or loads them. If you only have a time of day, but still need timezone support, attach them to today's date, so you get the right timezone. Only convert to strings again as late as possible.
in Mongodocs, it is said that datetimes with specified "Z" timezone at the end are saved as "UTC" datetime format..
https://docs.mongodb.com/manual/reference/method/Date/
I created some sample time data in Python:
now=str((datetime.datetime.now()).isoformat())+'Z'
then=str((datetime.datetime.utcnow()+datetime.timedelta(0,one_week_in_seconds)).isoformat())+'Z'
I used datetime.now() and datetime.utcnow() and appended 'Z' on both...
this is what I get:
'now': '2018-07-10T11:06:05.512484Z',
'then': '2018-07-17T09:06:05.512484Z',
I am now using MEAN stack with Node/Express and mongoose (ODM) Driver to make my schema models in the database. When I push the data via some router middleware to my mongoDB Database, the two fields have mongoose "Date" format. However, for both fields it creates an ISODate time format...:
"now" : ISODate("2018-07-10T09:02:01.410Z"),
"then" : ISODate("2018-07-17T09:02:01.410Z"),
I think thats a bug, normally, if "Z" is specified, it should create the specified time in ISO-Format, which it is here, but as I have created the time in local-time format and appended a "Z" in the first case ("now"), the time should be saved as 'now' : ISODate("2018-07-10T11:06:05.512484Z") without modifying /
converting from local to UTC time or not?
if you want to save specific country timezone and if you are using moment then it is easy to manage
below link will help to you
Updating time offset with moment().utcOffset()