3

I have the following code:

// 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();
    ...

Here is the output of Log::info(print_r($company_ids, true))

[2016-10-22 02:41:27] production.INFO: Array
(
    [0] => 57515764b91a8c4d008b45d1
    [1] => 57515764b91a8c4d008b45d6
    [2] => 57515764b91a8c4d008b45db
    [3] => 57515764b91a8c4d008b45e0
    ...
)

How can I query the Archive collection directly using company_ids and removing the need for the foreach loop?

Nissa
  • 4,636
  • 8
  • 29
  • 37
Edwin
  • 886
  • 2
  • 13
  • 32
  • have you tried just $results = Archive::where("billing.company._id", 'all', $company_ids)->get(); – Eric Oct 21 '16 at 01:06
  • Yes I tried that, but the result is empty. – Edwin Oct 21 '16 at 01:34
  • try maybe $results = Archive::where("billing.company._id", 'all', json_decode('{"$in":'+json_decode($company_ids)+'}')->get(); – Eric Oct 21 '16 at 12:39
  • That doesn't work as well; phpStorm complained about the wrong concatenation operator, and the 2nd json_decode errored out expecting string, which http://stackoverflow.com/questions/7436925/json-decode-expects-parameter-1-to-be-string-array-given solved. This is what I end up with: Archive::where("billing.company._id", 'all', json_decode('{"$in":' . json_encode($company_ids) . '}')->get()); but it still errors out "Call to a member function get() on array" – Edwin Oct 22 '16 at 02:47

2 Answers2

1

A small update in @Robbie answer. No need to use an array of MongoIds simply use array of string and it will work. Am using this only with laravel-jenessengers

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

$results = Archive::whereIn('billing.company._id', $company_ids)->get();
Haridarshan
  • 1,898
  • 1
  • 23
  • 38
0

According to the readme for the library:

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

(I'm not clear on what array $company_ids is, but I presume it needs to be and array of MongoID: but this also depends on the underlying library you are using, Mongo or MongoDB, as both are supported by the library)

$company_ids = [
    new mongoID('57515764b91a8c4d008b45d1'),
    new mongoID('57515764b91a8c4d008b45d6'),
    new mongoID('57515764b91a8c4d008b45db'),
    new mongoID('57515764b91a8c4d008b45e0'),
]
Robbie
  • 17,605
  • 4
  • 35
  • 72
  • Tried this but it doesn't work, $results is empty. However a similar query that doesn't compare a subdocument will work. – Edwin Oct 26 '16 at 01:08
  • Actually, just re-reading your structure, `billing.company._id` looks like it's not a MongoID but is your own personal ID? If so, just pass in the array if IDs as strings. Only use new `MongoID()` if you're looking for the MongoID (`_id`) column. – Robbie Oct 26 '16 at 05:15