简体   繁体   中英

Mongodb populate aggregated field

I want to use $lookup for joining 3 different collections in mongoDB, and populate a ref array with the coresponding data of the ref which is the client collection.

Conversation:

{
  _id: mongoose.Schema.Types.ObjectId,
  participants: [{ type: mongoose.Schema.Types.ObjectId, ref: "client" }],
  created_at: { type: Date, default: Date.now },
}

Message:

{
  _id: mongoose.Schema.Types.ObjectId,
  conversation_id: { type: mongoose.Schema.Types.ObjectId, ref: "Conversation",
  message: { type: String, default: "" },
  sender: {type: mongoose.Schema.Types.ObjectId,ref: "Client",default: null},
  reciever: {type: mongoose.Schema.Types.ObjectId,ref: "Client",default: null,},
  created_at: { type: Date, default: Date.now }
}

Client:

{
  _id: mongoose.Schema.Types.ObjectId,
  email: {type: String, required: true, unique: true},
  name: { type: String, default: "" },
  job: { type: String, default: "" },
  company: { type: String, default: "" },
  school: { type: String, default: "" },
}

Here is what i am usgin now without the populate:

  Conversation.aggregate([
    { $match: { participants: { $all: [mongoose.Types.ObjectId(myId)] } } },
    {
      $lookup: {
        from: "messages",
        localField: "_id",
        foreignField: "conversation_id",
        as: "messages",
      },
    },
    {
      $unwind: { path: "$messages", preserveNullAndEmptyArrays: true },
    },
    { $sort: { "messages.created_at": -1 } },
    {
      $group: {
        _id: "$_id",
        messages: { $first: "$messages" },
        doc: { $first: "$$ROOT" },
      },
    },
    { $replaceRoot: { newRoot: "$doc" } },
  ])

This is the result i get:

[
 {
  "_id": "5f29ca6fc410a01e6fd53ca1",
  "participants": [
   "5f1c4685546a7741b8c6e801",
   "5f1c80d1f52da506603278f1"
  ],
  "messages": {
   "_id": "5f2aff3715db59002458f650",
   "message": "Thanks man",
   "sender": "5f1c80d1f52da506603278f1",
   "reciever": "5f1c4685546a7741b8c6e801",
   "conversation_id": "5f29ca6fc410a01e6fd53ca1",
   "created_at": "2020-08-05T18:49:27.752Z",
   "__v": 0
  }
 },
 {
  "_id": "5f29c944c410a01e6fd4aa2b",
  "participants": [
   "5f1c4685546a7741b8c6e801",
   "5f05d5e20db0174bd4b55b29"
  ]
 }
]

I want to populate the participants array when i aggregate the conversation with message .

Thank you.

Using aggregate() ,

  • here you can use lookup with pipeline , it can to add all pipelines lookup level,

  • no need to $unwind and $group

Conversation.aggregate([
  {
    $match: {
      participants: {
        $all: [
          mongoose.Types.ObjectId(myId)
        ]
      }
    }
  },
  • lookup for messages
  {
    $lookup: {
      from: "Message",
      let: {
        conversation_id: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$$conversation_id",
                "$conversation_id"
              ]
            }
          }
        },
        {
          $sort: {
            "created_at": -1
          }
        }
      ],
      as: "messages"
    }
  },
  • lookup for participants
  {
    $lookup: {
      from: "Client",
      let: {
        participants: "$participants"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$_id",
                "$$participants"
              ]
            }
          }
        },
        {
          $sort: {
            "created_at": -1
          }
        }
      ],
      as: "participants"
    }
  }
])

Playground: https://mongoplayground.net/p/Pz5ojJ_dGGY

Note: I have assumed the name of the collections so you can correct and change as per your requirement.

this is the code i got, i changed the id to string only:

Conversation.aggregate([
    { $match: { participants: { $all: [mongoose.Types.ObjectId(myId)] } } },
    {
      $lookup: {
        from: "messages",
        let: { conversationId: { $toObjectId: "$_id" }},
        pipeline: [
          {
            $match: { $expr: {$eq: ["$$conversationId", "$conversation_id"]}},
          },
          {
            $sort: { create_at: -1 },
          },
          { $limit: 1 },
        ],
        as: "messages",
      },
    },
    {
      $lookup: {
        from: "clients",
        let: {participants: "$participants"},
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [
                  { $in: ["$_id", "$$participants"] },
                  { $ne: ["$_id", mongoose.Types.ObjectId(myId)] },
                ]},
            },
          },
          {
            $sort: { created_at: -1 },
          },
        ],
        as: "participants",
      },
    },
    {
      $project: {
        _id: 1,
        messages: 1,
        "participants.name": 1,
        "participants.company": 1,
        "participants.school": 1,
      },
    },
  ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM