D
D
Dmitry2019-07-11 08:28:50
PHP
Dmitry, 2019-07-11 08:28:50

MongoDB. How to query array elements?

Let's say we have an array:

'Pages': {
    {
      'title' => 'Page title 1',
      'tags' => ['Images', 'Movies']
    },

    {
      'title' => 'Page title 2',
      'tags' => ['Travels']
    },

    // ...
}

How to do a '$in' query on the 'tags' field for each element of an array? Is there an operator in mongodb used in a field name that stands for any numeric index? For example: pages.*.tags $in ['Movies', 'Travels'].

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2019-07-11
@res2001

You can use aggregate, and then $unwind over the array.
At the end, if necessary, you can regroup.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question