1

我有 2 个集合:文档和类型。在文档中我有文档:

{
    '_id': ObjectId('0000'),
    'name': 'someName',
    'type': ObjectId('1111') // id i have
}

在类型中:

{
    '_id': ObjectId('2222'),
    'for': 'Docs',
    'types': [
        {
            '_id': ObjectId('1111'), // type i need
            'name': 'someType' // want to get this name
        }
         // , and so on
    ]
}

Docs.findOne({_id: '0000'})我拥有type = ObjectId('1111')('someType'的id)之后。获取 someType 的名称值的最佳方法是什么?

4

1 回答 1

0

您可以$在字段选择参数中使用位置运算符findOne来仅提取匹配元素:

Types.findOne({'types._id': someTypeId}, {'types.$': 1}, function (err, type) {
    if (type) {
        console.log('Type name is: ' + type.types[0].name);
    }
});
于 2012-10-10T13:22:53.383 回答