D
D
DeniSidorenko2020-10-27 12:30:27
MongoDB
DeniSidorenko, 2020-10-27 12:30:27

How to implement link function in mongodb?

Given. Product creation page and category creation page.
They have a circuit trace

const {Schema, model, Types} = require('mongoose')

const schema = new Schema({
  name: {type: String, required: true},
  price: {type: Number, required: true},
  rating: {type: Number, default: 4.7},
  weight: {type: String, required: true},
  image: { type: String, trim: true},
  category: {type: Types.ObjectId, ref: "MyCategory"}
})


module.exports = model("MyProduct", schema)


const {Schema, model, Types} = require('mongoose')

const schema = new Schema({
  name: {type: String, required: true, unique: true},
  image: {type: String, required: true},
  owner: [{
    type: Types.ObjectId,
    ref: "MyProduct"
  }]
})

module.exports = model("MyCategory", schema)


When adding a product, I made a select, which has all the categories, with the value of their id (I get the categories using the get method from the database)
And for the product, everything is formed more or less correctly . I think that is not correct, because the categories themselves do not add the product to their owner UPDATE array. Yes, of course, you can go to the category page, get the category id, then all the products, and filter by category id. But I think it would be more correct if the product id was immediately in the owner array
image.png

image.png

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