【发布时间】:2019-04-19 06:50:49
【问题描述】:
我正在使用 lumen 和 mongodb
我想根据文件夹获取(未读总数)和消息总数
我的 mongodb 查询如下所示,
$totalEmails = DB::connection('mongodb')
->select("sum(if(is_read==0,1,0)) as unread", "count(message_id) as total", "folder_id")
->collection('email_message')
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
它给了我如下错误,
FatalErrorException in Connection.php line 333:
Call to a member function prepare() on null
请帮我解决这个问题。谢谢。
我已通过如下更改查询来解决该错误,
$totalEmails = DB::connection('mongodb')
->collection('email_message')
->select("sum(if(is_read==0,1,0)) as unread", "count(message_id) as total", "folder_id")
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
但它没有给我预期的结果,
它给我的结果如下,
[_id] => Array
(
[folder_id] => 5bee461e19f043020c001844
)
[folder_id] => 5bee461e19f043020c001844
[sum(if(is_read==0,1,0)) as unread] =>
[count(message_id) as total] =>
但我的期望是
[_id] => Array
(
[folder_id] => 5bee461e19f043020c001844
)
[folder_id] => 5bee461e19f043020c001844
[unread] => 2
[total] => 10
你能告诉我查询哪里出错了吗???
如果我使用下面的查询
$totalEmails = DB::connection('mongodb')
->selectRaw("sum(if(is_read==0,1,0)) as unread", "count(message_id) as total", "folder_id")
->collection('email_message')
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
它给了我这样的错误,
ErrorException in Builder.php line 245:
Argument 2 passed to Illuminate\Database\Query\Builder::selectRaw() must be of the type array, string given,
如果我使用以下查询,
$totalEmails = DB::connection('mongodb')
->collection('email_message')
->selectRaw("sum(if(is_read==0,1,0)) as unread,count(message_id) as total,folder_id")
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
它给了我这样的结果,
[_id] => Array
(
[folder_id] => 5bee461e19f043020c001844
)
[folder_id] => 5bee461e19f043020c001844
[sum(if(is_read==0,1,0)) as unread,count(message_id) as total,folder_id] =>
请帮助我获得预期的结果
【问题讨论】: