D
D
DeniSidorenko2021-04-05 10:11:59
MongoDB
DeniSidorenko, 2021-04-05 10:11:59

How to get an element by ID(objectId) if it is inside another model?

Good afternoon, there is a Restaurant model where there is an array of products that has its own ObjectID. The question is how to do a search exactly by that ObjectID in order to get its fields (price, names, etc.)
I tried all the methods but it doesn’t work, and I have a feeling that it won’t be possible to get exactly the object if it is nested
. For example, properties

Restaurant.find({products: {$elemMatch: {"_id": product_id}}})

Outputs the result of the restaurant that has an array of products with this ID

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