Hello,i’m creating a project management web app using MEAN Stack and i’m struggling with updating a project and multiple users, when adding/removing participants (users). So i have this two collections, users and projects.
Heres is the users schema:
name: {type: String, required="true" } ,
email: {type: String, unique:true, required="true",lowercase:true } ,
password: {type: String, required="true"} ,
projects: [{type: Schema.Types.ObjectId ,required:false,ref:'projects'}]
},{collection:'users'});
this is the projects schema:
name:{type: String, required:true,unique:true},
description: {type: String, required:true},
creator: {type: Schema.Types.ObjectId,required:false,ref:'users'},
lastEditor:{type: Schema.Types.ObjectId,required:false,ref:'users'},
participants:[{type: Schema.Types.ObjectId ,required:false,ref:'users'}],
files: [{type: Schema.Types.ObjectId,required:false, ref:'files'}]
},{collection:'proyectos'});
I’m thinking about the next aproach:
when editting the project i’m using an array of users objects to add/remove these users, so when i submit the form i get the whole project object in req.body
i’m thinking about using a foraeach to push the users _id into participants array from the users object, but here is where i got lost, i don’t know how to push the project id into all the users from the given _id in the foreach loop. deleting i thinks is about the same way, splicing the _id from participants array it should remove the project from user. i’ll apreciate all your help with this