N
N
Nikolai2019-02-03 22:20:28
Design
Nikolai, 2019-02-03 22:20:28

How to find documents in a collection in MongoDB by a field containing an array of id of other documents?

{
        "_id" : ObjectId("5c56f44a72e502aa8d9ecbef"),
        "parent_id" : [ObjectId("5c56f44a72e502d9ecbefaa8"), ObjectId("5c56f44a72d9ecbefaa8e502") ],
        "url" : "category-ochki-solncezashhitnye",
        "name" : "Очки",
        "subname" : "Для всей семьи",
        "keywords" : "солнцезащитные очки",
        "description" : "Lorem Ipsum ....",
        "image" : "140d8ed.png",
        "timestamp" : 1388939670,
        "position" : 0,
        "isDisabled" : false
}

Here is an example document.
And let's say I need to find categories that have a parent category, from which I only know the url field.
How can I do it?
The conditional option is as follows:
1. Find the id of the category whose url I know.
2. Find categories whose parent_id contains the id from step 1.
What is the easiest way to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Kudryavtsev, 2019-02-04
@Deissh

It is very important to be able to google. The first issue of Google.
https://docs.mongodb.com/manual/tutorial/query-arrays/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question