so I'm trying to do a simple Thumbs Up system for an App I'm working on, but I'm having issues pushing the User ID to the likes Array. Heres what my code looks like:
Room Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const classroomSchema = Schema({
clName: String,
clPosts: [{
title: String,
mssg: String,
date: String,
imageurl: String,
HERE==> likes: [{type: Schema.Types.ObjectId, ref: 'User'}]
}]
})
const Classroom = mongoose.model('Classroom', classroomSchema)
module.exports = Classroom
router
router.put('/put/classroom/post/like/:id', (req, res, next) => {
var data = data = req.body
Classroom.findByIdAndUpdate(
{
_id: req.params.id,
"clPosts": { _id: data.post }
},
{ $push: { "clPosts.$[outer].likes": [data.user] } },
{ "arrayFilters": [{ "outer._id": data.post }] }
).then((room) => {
console.log("Done");
res.json(room);
}).catch(next)
})
I've tried following other suggestions here on SO but I'm not sure if I have something configured incorrectly or there is a better way of pushing an object into a nested array.
The Basic setup is, there is a Classroom Collection holding Classroom Objects. within The Classroom, there is posts Object Array, and inside that, a likes Array. The idea is whenever someone Likes the post, it saves the ObjectID of that user into the array, which I then use to count how many likes, etc...
If any more detail is needed please let me know, MongoDB doesn't have good documentation on nesting arrays.