$lookup 返回空数组 [英] $lookup returning empty array

查看:27
本文介绍了$lookup 返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学生集合和一个班级集合student._id(ObjectId) 需要在 classes.owner(String) 上的文件集合的查找中匹配.我得到文件的空数组.我做错了什么?

I have a students collection and a classes collection student._id(ObjectId) needs to match in a lookup of the files collection on classes.owner(String). I am getting empty array for files. What am I doing wrong?

var express = require('express');
var router = express.Router();
var Account = require('../models/account');
var Connections = require('../models/connections');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;


//get all advocates for student.
router.route('/:student_id?')

  .get(function (req, res) {

    Account.aggregate(
      { "$match": { "_id": mongoose.Types.ObjectId(req.params.student_id) } },
      {$lookup:
        {
          from: 'classes',
          localField: 'owner',//**String**
          foreignField: '_id', //**ObjectId**
          as: 'classes'
        }
      }       
    ).exec(function (err, doc) {
        console.log(doc[0]);
        if (err) {
          res.send(err);
        } else {
          res.send(doc[0]).end();
        }
      })
  });


module.exports = router;

推荐答案

检查你 from 子句,是你的集合名称吗?我使用了模式名称而不是集合名称,这导致结果为空.

check you from clause, is it your collection name? I used schema name instead of the collection name which results in empty result.

{$lookup:
        {
          from: 'classes',  //check this
          localField: 'owner',//**String**
          foreignField: '_id', //**ObjectId**
          as: 'classes'
        }
      }   

这篇关于$lookup 返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆