I have a collection named Product on MongoDB with some documents, here is an example:
{
_id: 'xxxxxx',
name: 'cellphone',
brands: [
'aaaaaa',
'bbbbbb'
]
}
The 'brands' key makes reference to another collection named Brand, example:
[
{
_id: 'aaaaaa',
name: 'Apple',
_deprecatedDate: null
},
{
_id: 'bbbbbb',
name: 'BlackBerry',
_deprecatedDate: '2016-07-13T02:27:17.724Z'
}
]
So, with a Product id, I want to get all it's non-deprecated brands. The only way I found to do that is with the following code:
let _product = await Product.findOne({ _id: 'xxxxxx' });
return Brand.find({ _id: { $in: _product.brands }, _deprecatedDate: null });
Is there a way to do that with one single query?