mongoose 쿼리를 그룹화하고 mongoose에서 $ lookup을 사용하여 몇 개의 하위 문서를 채우려고하지만 결과 문서 배열이 비어있는 상태로 반환됩니다. 누구나 그 이유를 말해 줄 수 있습니까? ```집계 및 조회가 작동하지 않습니다.
module.exports = {
index: function(req, res, next){
Poll.findOne({_id: req.params.poll_id}, function(err, poll){
if(err) return next(err);
console.log(poll);
Answer.aggregate([
{$unwind: "$_question"},
{$match: {_poll: poll._id}},
{$group: {
_id: '$_question',
}},
{
$lookup : {
from : 'polls',
localField : '_poll',
foreignField: '_id',
as: 'poll'
},
},
{
$lookup : {
from : 'questions',
localField : '_question',
foreignField: '_id',
as: 'questions'
},
},
{
$lookup : {
from : 'users',
localField : '_user',
foreignField: '_id',
as: 'user'
},
},
], function (err, result) {
res.status(200).json(result);
});
});
},
The new subdocuments are returned empty for some reason. Please note that each answer contains the reference to ONE poll, ONE user and MANY questions.
[
{
_id: "57ce8927ea5a4f090774215d",
poll: [ ],
questions: [ ],
user: [ ]
}
]
```:
은```여기
```
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var answerSchema = new Schema({
_user : { type: Schema.Types.ObjectId, ref: 'User', required: true },
_poll : { type: Schema.Types.ObjectId, ref: 'Poll', required: true },
_question : [{ type: Schema.Types.ObjectId, ref: 'Question' }],
answer : { type : String },
},
{ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
});
mongoose.model('Answer', answerSchema);
내 코드입니다누구든지 내 실수를 감지 할 수 있습니까?
대신 $ project를 사용해야합니까? $ 조회가 새롭고 더 나은 방법이라고 들었습니다. mongo 3.2.0과 mongoose 4.5.8을 사용하고 있습니다.
미리 감사드립니다.
Github-flavored Markdown (트리플 백틱 코드 섹션)은 여기서 작동하지 않습니다. 코드 섹션을 4 개의 스페이스로 들여 쓰기하십시오 (이에 대한 편집기 버튼이 있습니다) – Tomalak