Mongoose聚合返回不需要的空数组

时间:2017-03-18 14:25:17

标签: node.js mongodb mongoose

我有两个型号;一个用于用户,另一个用于 StudyGroup 。每个 StudyGroup 都有一个唯一的guid字段。 用户模型有一个studyGroups字段,它是一个字符串guid数组。用户可以加入多个学习小组。

用户模型

 const userSchema = new Schema({
  cuid: { type: 'String', required: true },
  firstName: { type: 'String', required: true },
  lastName: { type: 'String', required: true },
  studentId: { type: 'Number', required: true },
  password: { type: 'String', required: true },
  email: { type: 'String', required: true },
  dateAdded: { type: 'Date', default: Date.now, required: true },
  lastLogin: { type: 'Date', default: null, required: false },
  studyGroups: [{ type: 'String' }],
});  

StudyGroup 模型

const studyGroupSchema = new Schema({
  guid: { type: 'String', required: true },
  groupName: { type: 'String', required: true },
  course: { type: 'String', required: true },
  teacher: { type: 'String', required: true },
  description: { type: 'String', required: true },
  dateAdded: { type: 'Date', default: Date.now, required: true },
  chatMessages: [{ type: 'String' }],
});

我想使用存储在数组中的字符串studyGroup User 模型的guid数组中找到所有studyGroups。然后将相应的studyGroup对象发送到前端。

export function getUserStudyGroups(req, res) {
  User.aggregate([
    { "$unwind": "$studyGroups" },
    {
      "$lookup": {
        "from": "studyGroups",
        "localField": "studyGroups",
        "foreignField": "cuid",
        "as": "resultingStudyGroupsArray"
      }
    },
    { "$unwind": "$resultingStudyGroupsArray" },
    {
      "$group": {
        "_id": null,
        "myStudyGroups": { "$addToSet": "$resultingStudyGroupsArray" },
        "count": { "$sum": 1 }
      }
    }
  ]).exec(function(err, results){
    console.log(results);
    return res.json({ studyGroups: results });
  });
}

但是,上面的代码只返回一个空数组。但我想返回一组studyGroup对象。

2 个答案:

答案 0 :(得分:1)

我设法从用户模型中获取所有guid而不使用聚合。

export function getUserStudyGroups(req, res) {
  User.findOne({ cuid: req.params.cuid }).select('studyGroups').exec((err, studyGroups) => {
    if (err) {
      return res.status(500).send(err);
    }
    console.log(studyGroups)
    StudyGroup.find({ guid: {$in: studyGroups.studyGroups }}).exec((err, foundGroups) => {
      if (err) {
        return res.status(500).send(err);
      }
        return res.json({ myGroups: foundGroups });
      });
    });
}

User和StudyGroup引用他们的相应模型,studyGroups.studyGroups返回一个字符串数组(guid' s)

答案 1 :(得分:0)

foreignField应为guid

export function getUserStudyGroups(req, res) {   User.aggregate([
    { "$unwind": "$studyGroups" },
    {
      "$lookup": {
        "from": "studyGroups",
        "localField": "studyGroups",
        "foreignField": "guid",
        "as": "resultingStudyGroupsArray"
      }
    },
    { "$unwind": "$resultingStudyGroupsArray" },
    {
      "$group": {
        "_id": null,
        "myStudyGroups": { "$addToSet": "$resultingStudyGroupsArray" },
        "count": { "$sum": 1 }
      }
    }   ]).exec(function(err, results){
    console.log(results);
    return res.json({ studyGroups: results });   }); }