本文介绍了$ lookup返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个学生集合和一个班级集合student._id(ObjectId)需要在classes.owner(String)上的文件集合的查找中匹配.我正在获取文件的空数组.我在做什么错了?
I have a students collection and a classes collectionstudent._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;
推荐答案
从子句检查您,它是您的集合名称吗?我使用架构名称而不是集合名称,这导致结果为空.
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返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!