L
L
lssssssssssl2021-07-21 20:02:45
MongoDB
lssssssssssl, 2021-07-21 20:02:45

How to link schemas in mongodb?

What I need? For example, there are two schemas user and post. I want to build the following relationship between them: I save the user ID in the post - postModel.create (data), and when I then make a request to the user - userModel.findById (id), I want to get, among other information about the user, the posts field, which will contain information about the previously saved post of this person.

I know that you can save posts directly to the user model and make a link in the schema in the form
[{type: mongoose.Schema.Types.ObjectId, ref: 'Posts'}], but I don't want to access the user model every time I update posts (Or is this the only way in my case?) - I want to access the post model and then see the changes in the user.

I don't have much experience with mongo, I don't understand how to humanly connect these schemas.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question