S
S
stolichna92020-07-06 07:35:10
MongoDB
stolichna9, 2020-07-06 07:35:10

What is the best way to store data about objects from another mongodb collection?

There are two collections. In one product and its description, and in another collection, the buyer. I store the basket as an array in the customer. I don't know how better. In the basket to store links (objectid) to goods from the collection of goods or to create the objects themselves in the basket?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-07-06
@hzzzzl

I think it's better to store links to products, and not copy products - otherwise you can not keep track of information changes, and the user will display outdated information, well, in general,
if this is going to node / mongoose, then I would start with the fact that in the user model made

cart: [{ 
  type: mongoose.Schema.Types.ObjectId, 
  ref: 'Item' 
  // массив айдишников айтемов из коллекции items
}]

and upon request would populate this array cart
User.findOne({ _id: '......user id ......' }).populate('cart')

https://mongoosejs.com/docs/populate.html
further - by circumstances ¯\_(ツ)_/¯

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question