I have 2 collections: Docs and Types. In Docs i have document:
{
'_id': ObjectId('0000'),
'name': 'someName',
'type': ObjectId('1111') // id i have
}
In Types:
{
'_id': ObjectId('2222'),
'for': 'Docs',
'types': [
{
'_id': ObjectId('1111'), // type i need
'name': 'someType' // want to get this name
}
// , and so on
]
}
After Docs.findOne({_id: '0000'})
i have type = ObjectId('1111')
(id of 'someType'). What is the best way to get someType's name value?
You can use the $
positional operator in your field selection parameter to findOne
to pull out just the matching element:
Types.findOne({'types._id': someTypeId}, {'types.$': 1}, function (err, type) {
if (type) {
console.log('Type name is: ' + type.types[0].name);
}
});