K
K
KondakovVladimir2020-05-02 15:02:05
MongoDB
KondakovVladimir, 2020-05-02 15:02:05

What is the correct way to use the aggregate function to combine data from multiple collections?

Hello.
There are several collections
Users
Team members (where there is a link to a user)
Team messages (where there is a link to a team member)

how to properly build a pipeline to collect related data on a user

Пользователь
  - члены команд
    - сообщения команд


db['users'].aggregate([
        {"$match":{"chat_id":user.chat_id}},
        {"$lookup":{
            "localField" : "_id",
            "from" : "team_members",
            "foreignField" : "user_id",
            "as":"member" 
        }}])

logic suggests that instead of "team_members" you need to use a nested pipeline

, maybe I'm driven and there is nothing criminal in the phased formation (first select team members, and then attach everything you need to this particular collection)

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