0

The DB has 9 users (collection 1). Each user can have 0 or 1 rsvps (collection 2). There are only 2 rsvps in the rsvps collection.

The following code returns 9 records. Even for the 2 users with rsvps, rsvp is an empty array [ ].

What am I doing wrong?

    //  Gets all Users' attendance information for the Home view.
    return new Promise( (resolve, reject) => {

        // Get all attendance database records using mongoose.
        User.aggregate([{
            $lookup: {
                from: "rsvps", // collection name in db
                localField: "_id",
                foreignField: "userId",
                as: "rsvp"
            }
        }
        ]).exec( (err, userList) => {
            if (err) throw err;

            console.log(userList);
        });
Jeff Matthews
  • 602
  • 2
  • 7
  • 17
  • `_id` and `userId` will actually have data of different types then. The common mistake is that your `_id` is an `ObjectId` value and the `userId` actually contains a "string". Typically because you did not realize there was a difference at all. View the documents in the `mongo` shell, where it will show you the clear distinction. – Neil Lunn Nov 15 '17 at 05:19
  • I see. How do I get these to compare properly? – Jeff Matthews Nov 15 '17 at 05:26
  • By making them the same type. You cannot match an `ObjectId` to a "string". Thought I just said that. `1` is not equal to `"1"` if that's a simpler concept to grasp. Fix your data. – Neil Lunn Nov 15 '17 at 05:28

0 Answers0