3

我有以下代码:

// 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循环的需求?

4

2 回答 2

1

@Robbie答案中的一个小更新。无需使用MongoIds简单地使用字符串数组的数组,它就可以工作。我只使用 laravel-jenessengers

$company_ids = [
    '57515764b91a8c4d008b45d1',
    '57515764b91a8c4d008b45d6',
    '57515764b91a8c4d008b45db',
    '57515764b91a8c4d008b45e0'
]

$results = Archive::whereIn('billing.company._id', $company_ids)->get();
于 2016-10-29T10:58:26.007 回答
0

根据图书馆的自述文件:

$results = Archive::whereIn('billing.company._id', $company_ids)->get();

(我不清楚 $company_ids 数组是什么,但我认为它必须是 MongoID 数组:但这也取决于您使用的底层库MongoMongoDB,因为两者都受库支持)

$company_ids = [
    new mongoID('57515764b91a8c4d008b45d1'),
    new mongoID('57515764b91a8c4d008b45d6'),
    new mongoID('57515764b91a8c4d008b45db'),
    new mongoID('57515764b91a8c4d008b45e0'),
]
于 2016-10-25T23:44:45.107 回答