我有学生收藏和课程收藏
student._id(ObjectId)需要匹配classes.owner(String)上的文件集合的查找.我正在获取文件的空数组.我究竟做错了什么?
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;
解决方法:
检查你的条款,这是你的收藏品名称?我使用模式名称而不是集合名称导致空结果.
{$lookup:
{
from: 'classes', //check this
localField: 'owner',//**String**
foreignField: '_id', //**ObjectId**
as: 'classes'
}
}