'Laravel mongodb where array field contains value

I use mongodb and mysql together with Laravel. Fields table in mysql database can have more options in mysql database.

  • fields table in mysql: id, name
  • options table in mysql: id, field_id, name

so in mongodb database I have adverts table and this adverts table have field and field equal to options something like this for example {field_id_1: [option_id, option_id]}. When I use it like this there is no problem when querying the database. It is simple like this $advert->whereIn('field_id_1', $request->options).

but then I decided why not to keep this options as an array in mongodb and without field_id like this {options: [option_id, option_id, option_id]}. So there is the question now I somehow want query the database where this options field contains requests value something reverse of $adverts->whereIn('field', $options)

I know a way but not sure

$adverts->where('options', 'LIKE', '%"'.$option_id.'"%');


Solution 1:[1]

I don't know if this answer will work for you :

$result = $adverts->where("options", "all", [$option_id])->get();

or :

$result = $adverts->where("options", "all", "$options_ids")->get();

where $options_ids are an array of options

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Achraf BOUCETTA