I
I
IDONTSUDO2019-07-09 17:24:45
MongoDB
IDONTSUDO, 2019-07-09 17:24:45

What is the best way to organize a collection of data in mongose?

I'm just doing an online store. The store has a manager. He enters data about the goods. I don’t know how many goods there will be.

product:{
        type: String,
        required: true
    }

In mongoose, the product schema looks something like this.
And there are users who log in and place an order. Order chart below.
product1: {
        type: String //данные о заказе
    },
    quality1: {
        type: Number //количество товара
    },
    product2:{
        type: String
    },
    quality2: {
        type: Number
    },

So far, orders look like this.
How can I make sure I don't have to duplicate collection names? (product1,product2 etc).
spoiler

i.e. yes conditionally i can make one collection
order: {
order: "сапоги 1, кросовки 2" //и  преобразовывать данные в одну строку
}

But isn't there a better solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-07-09
@IDONTSUDO

probably something like make each order an array of objects

order: [{
  product: ObjectId,
  quantity: Number
}]

well, so that the ObjectId of the selected product comes from the front, and the products themselves lie in their own separate table
, then you can suffer for a long time with different $lookup $aggregate, etc. all this mongo, but here I already pass :/
https://docs.mongodb.com/manual/reference/operator...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question