我有以下代码:
// company_ids is an array of mongo IDs
// company_id is an array (with only 1 element) of mongo ID
foreach($company_ids as $company_id){
$results = Archive::where("billing.company._id", 'all', array($company_id))->get();
...
以下是Log::info(print_r($company_ids, true))
[2016-10-22 02:41:27] production.INFO: Array
(
[0] => 57515764b91a8c4d008b45d1
[1] => 57515764b91a8c4d008b45d6
[2] => 57515764b91a8c4d008b45db
[3] => 57515764b91a8c4d008b45e0
...
)
如何使用Archive
直接查询company_ids
集合并删除foreach
循环的需要?
答案 0 :(得分:1)
@Robbie 答案中的小更新。不需要使用MongoIds
数组只需使用字符串数组就可以了。我只使用laravel-jenessengers
$company_ids = [
'57515764b91a8c4d008b45d1',
'57515764b91a8c4d008b45d6',
'57515764b91a8c4d008b45db',
'57515764b91a8c4d008b45e0'
]
$results = Archive::whereIn('billing.company._id', $company_ids)->get();
答案 1 :(得分:0)
根据图书馆的readme:
$results = Archive::whereIn('billing.company._id', $company_ids)->get();
(我不清楚数组$ company_ids是什么,但我认为它需要和MongoID数组:但这也取决于您使用的基础库,Mongo或{{ 3}},因为两者都得到了库的支持)
$company_ids = [
new mongoID('57515764b91a8c4d008b45d1'),
new mongoID('57515764b91a8c4d008b45d6'),
new mongoID('57515764b91a8c4d008b45db'),
new mongoID('57515764b91a8c4d008b45e0'),
]