【问题标题】:mongoose Handling two ref ObjectID in one field to fetch each collection of the ObjectIdmongoose 在一个字段中处理两个 ref ObjectID 以获取 ObjectId 的每个集合
【发布时间】:2020-12-31 06:39:01
【问题描述】:

我是 mongoose 和 rest API 的新手,我在检索每个集合时遇到问题,请参阅另一个文档

所有这些架构都经过编辑以使其易于阅读,因为真实情况我有更多字段,我认为不需要显示

我有一个像这样的user schema 数据:

{
    "_id": "123"
    "username": "Paijo",
    "email": "paijo@gmail.com"
}
{
    "_id": "345"
    "username": "Flix",
    "email": "Flix@gmail.com"
}

我有 message schema 这样的数据:

{
    "_id": "5f525b2ac1267e868e882fbc",
    "roomId": "123_345",
    "latestMessage": "Bisa lahh cujkkkk",
    "updatedAt": {
        "$date": "2020-09-05T07:00:27.846Z"
    },
    "createdAt": {
        "$date": "2020-09-04T15:20:10.840Z"
    }
}

我想查询roomId中是否有一些user _id并检索user schemamessage schema的所有字段。

假设我使用user _id 123 登录,我想在roomId 中获取包含123 的消息以及345 的详细用户,

我想要获取的预期结果是:

{
    "_id": "5f525b2ac1267e868e882fbc",
    "roomId": "123_345",
    "latestMessage": "Bisa lahh cujkkkk",
    "updatedAt": {
        "$date": "2020-09-05T07:00:27.846Z"
    },
    "createdAt": {
        "$date": "2020-09-04T15:20:10.840Z"
    },
    "username": "Flix", // get result from user schema which has _id 345
    "email": "Flix@gmail.com" // get result from user schema which has _id 345
}

目前我只能使用此查询获取包含用户登录的_id 的所有消息,但我不知道如何在roomId 中获取另一个ID 的详细用户

exports.listChat = async (req, res)=>{
  const userID = req.userID
  const listChat = await ChatRoom.find({roomId: new RegExp(userID)}).select("-message")
  console.log('listChat' , listChat)

  res.send(listChat)
}

【问题讨论】:

    标签: node.js mongodb express mongoose aggregation-framework


    【解决方案1】:

    您可以首先使用$split 并定义两个单独的子字段:me(当前用户)和other - 您要用于$lookup 的值。

    db.message.aggregate([
        {
            $addFields: {
                members: {
                    $let: {
                        vars: { mem: { $split: [ "$roomId", "_" ] } },
                        in: {
                            me: { $arrayElemAt: [ { $filter: { input: "$$mem", cond: { $eq: [ "$$this", "123" ] } } }, 0 ] },
                            other: { $arrayElemAt: [ { $filter: { input: "$$mem", cond: { $ne: [ "$$this", "123" ] } } }, 0 ] },
                        }
                    }
                }
            }
        },
        {
            $match: {
                "members.me": "123"
            }
        },
        {
            $lookup: {
                from: "user",
                localField: "members.other",
                foreignField: "_id",
                as: "user"
            }
        },
        {
            $unwind: "$user"
        },
        {
            $project: {
                _id: 1,
                roomId: 1,
                latestMessage: 1,
                updatedAt: 1,
                createdAt: 1,
                username: "$user.username",
                email: "$user.email",
            }
        }
    ])
    

    Mongo Playground

    【讨论】:

    • 关于您的查询,是否需要$project?我的意思是,我想显示所有字段而不是定义每个字段
    • @flix 不,这只是为了摆脱嵌套的子文档,但你不需要它
    猜你喜欢
    • 2016-06-13
    • 2017-03-16
    • 1970-01-01
    • 2017-03-30
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多