0
下面是我的樣品MongoDB的收集MongoDB的字符串到日期的轉換
{
"_id" : ObjectId("57ed32f4070577ec56a56b9f"),
"log_id" : "180308",
"issue_id" : "108850",
"author_key" : "priyadarshinim_contus",
"timespent" : NumberLong(18000),
"comment" : "Added charts in the dashboard page of the application.",
"created_on" : "2017-08-16T18:22:04.816+0530",
"updated_on" : "2017-08-16T18:22:04.816+0530",
"started_on" : "2017-08-16T18:21:39.000+0530",
"started_date" : "2017-08-02",
"updated_date" : "2017-08-02",
"role" : "PHP",
"updated_at" : ISODate("2017-09-29T15:27:48.069Z"),
"created_at" : ISODate("2017-09-29T15:27:48.069Z"),
"status" : 1.0
}
我需要記錄與started_date的幫助下,在默認情況下我會以這兩個日期我會檢查$gt
和開始$lt
日期。
$current_date = '2017-08-31';
$sixmonthfromcurrent ='2017-08-01';
$worklogs = Worklog::raw (function ($collection) use ($issue_jira_id, $current_date, $sixmonthfromcurrent) {
return $collection->aggregate ([
['$match' => ['issue_id' => ['$in' => $issue_jira_id],
'started_date' => ['$lte' => $current_date,'$gte' => $sixmonthfromcurrent]
]
],
['$group' => ['issue_id' => ['$push' => '$issue_id'],
'_id' => ['year' => ['$year' => '$started_date'],
'week' => ['$week' => '$started_date'],'resource_key' => '$author_key'],
'sum' => array ('$sum' => '$timespent')]
],
[ '$sort' => ['_id' => 1]
]
]);
});
如果我運行此查詢我得到這種類型的錯誤:
Can't convert from BSON type string to Date
如何糾正這個錯誤?
「started_date」:「2017-08-02」不是類型ISODate(),看看你在數據庫中的類型,它應該是字符串。 $ lte和$ gte應該在匹配查詢中工作。但是,它會在$ group操作中引發異常,因爲您嘗試從字符串中提取一週。 –
感謝您的回覆,如何在我的$ group操作中使用該字符串, – ganesh