D
D
DeniSidorenko2021-04-06 06:34:41
MongoDB
DeniSidorenko, 2021-04-06 06:34:41

Is it possible to perform such operations in Mongo?

Good afternoon, there are restaurants and categories. When adding a restaurant, the necessary categories are selected.
That is, the fields for the restaurant in this form

categories: [ { type: mongoose.Types.ObjectId, ref: "Category" } ]

And in this field there is a link to the category. However, when adding a category, I have exactly the same fields
const CategorySchema = new mongoose.Schema({
  name: { type: String, required: true, unique: true },
  slug: { type: String, required: true, unique: true },
  restaurants: [{ type: mongoose.Types.ObjectId , ref: "Restaurant" }]
})


However, the array of restaurants is empty, because the category is initially added and then the restaurant? The question is, is it somehow possible that when the category objectID is in the category array for a particular restaurant, that restaurant is in the restaurant array for the category.

I understand very well that MongoDB is not a real database, but I ask if this is possible using, for example, the same Middleware ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2021-04-06
@DeniSidorenko

Of course you can. Mongo is just designed to do this.
You understand everything perfectly, even with the help of the same Middleware.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question