我在和鲁门和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请帮我解决这个issue.thank你。
我已经通过更改查询来解决该错误,如下所示,
$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] => 请帮我得到我的预期结果
发布于 2018-11-16 05:26:52
我不确定这是否是导致您的问题的原因,但是您的代码中有一个错误。
它应该是:
$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();selectRaw需要在select中包含原始代码。这是获得更多信息的文档
https://stackoverflow.com/questions/53331812
复制相似问题