Mongodb填充汇总字段

时间:2020-08-06 09:20:15

标签: node.js mongodb mongoose

我想使用$ lookup在mongoDB中加入3个不同的集合,并用ref的核心响应数据(即客户端集合)填充ref数组。

对话:

$Date = "TEXT"
Write-Host "The script was started $Date"

$fileOut = Join-Path -Path $tsvEingang -ChildPath ("$Date.csv")
$MosaicSummary | Export-Csv -Path $fileOut -Delimiter "`t" -NoTypeInformation

#Result: The writtens csv-file-name is TEXT.csv

消息:

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

客户

{
  _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 }
}

这是我现在没有人口的地方:

{
  _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: "" },
}

这是我得到的结果:

  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" } },
  ])

当我将对话消息进行汇总时,我想populate个参与者数组。

谢谢。

2 个答案:

答案 0 :(得分:1)

使用aggregate()

  • 在这里您可以使用lookup with pipeline,它可以添加所有管道查找级别,

  • 不需要$ unwind和$ group

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

游乐场:https://mongoplayground.net/p/Pz5ojJ_dGGY

注意:我已经假定了集合的名称,以便您可以根据需要进行更正和更改。

答案 1 :(得分:0)

这是我得到的代码,我将id更改为仅字符串:

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,
      },
    },
  ])
相关问题