A
A
Alexey Shimanovich2021-04-25 23:01:25
Mongoose
Alexey Shimanovich, 2021-04-25 23:01:25

How to pass data from one $lookup to another?

There is an aggregate request that includes two $lookups:

[{
    $match: {
      id
    }
  },
  {
    $lookup: {      // 1
      from: 'blogs',
      as: 'blog',
      pipeline: [{
        $project: {
          id: 1,      // *           Отсюда
          name: 1,
          articles: 1
        },
      }, {
        $match: {
          articles: {
            $in: [id]
          }
        }
      }, {
        $unset: 'articles'
      }]
    }
  },
  {
    $lookup: {      // 2
      from: 'users',
      as: 'user',
      pipeline: [{
        $project: {
          id: 1,
          user_name: 1,
          picture: 1
        },
      }, {
        $match: {
          blogs: {
            $in: [...]      // Передать сюда
          }
        }
      }]
    }
  }
]


How can I transfer a field from the first to the second (the field is marked with *)?

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