I am using this package for my Laravel Application. I can get single collection data using this query:
$product = DB::connection( 'mongodb' )->collection( 'products_1' )->paginate( 5 );
Now, I have multiple collection for e.g:
products_2, products_3, products_4 and so on…
I want to get data from all the collection at once.
Could you tell me is there any way I can get ?
Thank You.
2
Answers
I feel the best way to go about this is to define the model for the collection having in mind that relational db uses tables while non-relational db like mongo uses collections, ill suggest you define your mongo collection in a model class.
Just like a normal model, the MongoDB model class will know which collection to use based on the model name. For Products, the collection Products will be used.
First, create a
Product
model:Then try to use
$unionWith
aggregation inwhereRaw
function:Ref:
If not work, try
raw
withtoQuery
:Ref: https://github.com/jenssegers/laravel-mongodb/issues/1574