* mongo (2.18.2)
* mongoid (8.0.3)
From Working With Data > Aggregation Pipeline:
band_ids = Band.collection.aggregate([
{ '$lookup' => {
from: 'tours',
localField: '_id',
foreignField: 'band_id',
as: 'tours',
} },
{ '$lookup' => {
from: 'awards',
localField: '_id',
foreignField: 'band_id',
as: 'awards',
} },
{ '$match' => {
'tours.year' => {'$gte' => 2000},
'awards._id' => {'$exists' => true},
} },
{'$project' => {_id: 1}},
])
bands = Band.find(band_ids)
I got an error:
> /Users/george/.rbenv/versions/3.1.2/gemsets/trading-app-2/gems/bson-4.15.0/lib/bson/hash.rb:43:in `put_hash': Value does not define its BSON serialized type: #<Mongo::Collection::View::Aggregation:0x0000000116166b08> (BSON::Error::UnserializableClass)
However, if I use #to_a, then it succeeds:
bands = Band.find(band_ids.to_a)
I'm not sure if this is desired behavior and the docs should be updated? Apparently it worked in the past so not sure...