问题
I have some dates that are string formatted like this in chats:
"_id" : ObjectId("2bfd5d45348ef655b5236d93"),
"status" : "closed",
"context" : "Chats",
"chats" : {
"time_initialized" : "2019-02-20T17:35:39.960284505Z",
"time_responded" : "2019-02-20T17:42:06.691469546Z",
"time_closed" : "2019-02-20T17:44:10.158421784Z"
}
My DB version in mongo is 3.6.5:
In the Mongo documentation, you can see below that the function dateFromString is supported:
Here's my code that is failing with:
"errmsg" : "Unrecognized argument to $dateFromString: format"
db.conversations.aggregate([
{$match: {"context": "Chats", "status": "closed",
"chats.time_closed": {$exists: true}}},
{$unwind: "$chats"},
{$project: {chats:1, _id: 1, status: 1,
initialised_closed_duration:
{$divide: [
{$subtract: [
{$dateFromString: {dateString: "$chats.time_closed", format: "%Y-%m-%dT%H:%M:%S.000000000Z"}},{$dateFromString: {dateString: "$chats.time_initialized", format: "%Y-%m-%dT%H:%M:%S.000000000Z"}}]},60000]}}},
{$limit: 5}]).pretty()
The point of my query is to get the date a chat is initialised and then closed, and work out the duration in minutes (hence dividing the millisecond date difference by 60,000)
Does anyone have any ideas about this?
回答1:
The operator was added in 3.6 but its signature changed in v4.
There was no "format" argument back in v3.6. The documentation for this version is available at https://docs.mongodb.com/v3.6/reference/operator/aggregation/dateFromString/
来源:https://stackoverflow.com/questions/54806248/why-am-i-getting-unrecognised-argument-to-datefromstring-format-despite-hav