1

I have two models; one for the User and the other one for the StudyGroup. Each StudyGroup has a unique guid field. The User model has a field of studyGroups that is an array of strings guid. A user can join multiple study groups.

User model

 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 model

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' }],
});

I want to find all studyGroups in the studyGroup array of the User model using the strings guid stored inside the array. Then send the corresponding studyGroup objects to the Front-end.

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 });
  });
}

However, the code above just returns me an empty array. But I want to return an array of studyGroup objects.

3
  • 1
    resultingTagsArray you haven't created resulting tags array anywhere. What is it? Commented Mar 18, 2017 at 15:09
  • woops, I forgot to change that. Let me see if that was the problem. Commented Mar 18, 2017 at 16:00
  • Still empty array after changing the resultingTagsArray to resultingStudyGroupsArray Commented Mar 18, 2017 at 16:04

2 Answers 2

1

I managed to fetch all the guid's from the User model without using aggregate.

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 and StudyGroup refer to their according model, and studyGroups.studyGroups returns an array of strings (the guid's)

Sign up to request clarification or add additional context in comments.

Comments

0

foreignField should be 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 });   }); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.