sql >> Base de Datos >  >> NoSQL >> MongoDB

Consulta anidada Mongoose en Modelo por campo de su modelo referenciado

No puede hacer esto en una sola consulta porque MongoDB no admite uniones. En su lugar, tienes que dividirlo en un par de pasos:

// Get the _ids of people with the last name of Robertson.
Person.find({lastname: 'Robertson'}, {_id: 1}, function(err, docs) {

    // Map the docs into an array of just the _ids
    var ids = docs.map(function(doc) { return doc._id; });

    // Get the companies whose founders are in that set.
    Company.find({founder: {$in: ids}}, function(err, docs) {
        // docs contains your answer
    });
});