-
Hi! I'm used to ORMs with the usual Coming from those traditional relational concepts I'm now thrown off by mongoose' It seems to me that using the same example I would have But now if User makes a new Post and sets It seems a bit weird to me that I need to update/amend the User record every time a new Post is being created since the lookup glue already exists in the Post schema... It feels counter intuitive coming from traditional relational databases... I just want to be sure this is the correct way and I'm not mixing things up or making it unnecessarily harder |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@f0o that is correct, you'd need to update A better alternative would be to store the reference in one place, ideally just |
Beta Was this translation helpful? Give feedback.
@f0o that is correct, you'd need to update
Post.user
andUser.posts
. Mongoose doesn't do that for you because the lookup glue is in Mongoose, not MongoDB.A better alternative would be to store the reference in one place, ideally just
User: {type: Schema.Types.ObjectId, ref: "User"}
, and makePosts
a virtual: https://mongoosejs.com/docs/tutorials/virtuals.html#populate