I’m using MongoDB version 3.4.17, accessed via PHP (MongoDB PHP API version 1.4). When the server writes a JSON document to the database, it first adds a timestamp field like so:
$record['date_modified'] = new MongoDBBSONUTCDateTime();
When reloading this record, which is serialized into a JSON, used by JavaScript, then passed back to the server to be saved with collection->replaceOne(), I get this error:
invalid argument for replace: keys cannot begin with "$": "$date""
The JavaScript console shows the date as an object with multiple levels of child objects:
date_modified:
$date:
$numberLong: "1572318771000"
Questions:
-
What should I be doing differently so I can save the JSON back to the database without it throwing the error it does for ‘keys cannot begin with "$"?
-
It would be handy for JavaScript to have the date in a more useful format. Like maybe a string that the JavaScript Date() method could take in its constructor. Should I be using one of these methods on the server side to convert it?
2
Answers
I ended up coming up with this code that I tested and it solves the problem. It does feel like a bit of a 'hack' and I'm not sure why it's required, but it does work. It looks for fields of the structure:
and converts it into a UTCDateTime.
How do you insert the data and how do you retrieve it?
I use this, and it works fine:
In order to convert a Mongo BSON date back to PHP Date, use MongoDBBSONUTCDateTime::toDateTime
If you need to work with JSON strings, then these commands would be useful:
Here is an example how they could be used: