Posts

Showing posts with the label relationship

Proper Mongoose Subdocument Relation

Proper Mongoose Subdocument Relation I have a model const userSchema = Schema({ username: String }) const invitationSchema = Schema({ decision: String, user: { type: Schema.ObjectId, ref: 'User' } }) const groupSchema = Schema({ name: String, invitations: [{ type: Schema.ObjectId, ref: 'Invitation' }] }) So the basic model looks like group { name invitations [ user { username } ] } As user i would like to get all groups that i have invitations for const user = getContextUser() Group.find({ 'invitations.user': user._id }) But it doesn't seem to work, i could use a property group referring to Group schema in Invitation model, but i want the good way approach Group Invitation How to i get all groups that user have invitations for Don't look at this below, this is my actual model representation in json, just in case // GROUP { "_id": ObjectId("5b3901dd9a8c0e6790af4ee8"), "i...