Mongoose, sort based on foreign/populated key

2019-01-28 22:27发布

问题:

I have two schemas(AgendaCompromissoSchema and profissionaisSchema) with mongoose.

I'd like to find a set of all AgendaCompromisso sorted by profissional.name descending.

I'd prefer separate schemas instead of nested documents.

I have tried the next code without success.

    router.get('/',function(req,res,next){  
        AgendaCompromisso.find({})
        .populate('profissional',name') //here I´d like the results sorted by 'name' descending
        .sort:{
           'profissional.name': -1 //Sort by name Added DESC
         }        
         .exec( (err,data) => {
           callback(err,data,res)
        })
    });

//router

const express = require('express');
const router = express.Router();
const AgendaCompromisso = require('../models/agendaCompromisso');
const callback=function(err,data,res){
     //console.log(data);
     if (err) return res.status(500).json(err);
     return res.status(200).send(data);
}   

//get all
router.get('/',function(req,res,next){  
    AgendaCompromisso.find({})
    .populate('profissional','name')
    .exec( (err,data) => {
       callback(err,data,res)
    })
});

//schemas

var AgendaCompromissoSchema = new mongoose.Schema({
  profissional:{type:mongoose.Schema.Types.ObjectId, ref:'Profissional'},
  title:{type:String},
  slot_dateInit:{type: Date},
  slot_timeInit:{type: Date}
});
module.exports = mongoose.model('AgendaCompromisso',  AgendaCompromissoSchema,'agendasCompromissos' );

var profissionaisSchema = new mongoose.Schema({
  name: {type: String, unique:true},
  cpf: {type: String},
});

module.exports = mongoose.model('Profissional', profissionaisSchema,'profissionais' );

回答1:

Using $lookup (which performs left outer join, similar to sql) in aggregation pipeline can solve this:

router.get('/',function(req,res,next){  
    AgendaCompromisso.aggregate([
    {
        $lookup:
        {
            from: "profissionais", //use the name of database collection not mongoose model
            localField: "profissional",
            foreignField: "_id",
            as: "profissional_doc"
        }
    },

    {
        $unwind: "$profissional_doc"  //remove array
    },

    {
        $sort: {"profissional_doc": -1}  // or {"profissional_doc": 1} for ascending
    }
    ])
    .exec( (err,data) => {
        callback(err,data,res)
    })
});

Just a note, for lookup you need mongodb version 3.2 or greater.