简体   繁体   English

返回不同集合猫鼬的字段

[英]Return fields from different collection Mongoose

Imagine a function that finds users by their name and returns them. 想象一个函数,该函数按用户名查找用户并返回用户。

 User.aggregate(
        [
            { $sort: { userFirstName: 1, userLastName: 1 } },
            {
                $addFields: {
                    firstLastName: { $concat: ['$userFirstName', ' ', '$userLastName'] },
                    lastFirstName: { $concat: ['$userLastName', ' ', '$userFirstName'] }
                }
            },
            {
                $match: $match // Set from above with match crit
            },
            {
                $group: {
                    _id: null,
                    total: { $sum: 1 },
                    data: {
                        $push: {
                            '_id': '$_id',
                            'userFirstName': '$userFirstName',
                            'userLastName': '$userLastName',
                            'userProfileImage': '$userProfileImage',
                            'userVihorCategory': '$userVihorCategory'
                        }
                    }
                }
            },
            {
                $project: {
                    total: 1,
                    data: { $slice: ['$data', start, limit] }
                }
            }
        ]
    ).exec((errAgg, results) => {...

This works, it splices them and returns them correctly. 此方法有效,它将它们拼接起来并正确返回它们。 There is another collection that tracks user connections. 还有另一个跟踪用户连接的集合。

{
    user: { type: Schema.Types.ObjectId, ref: 'User' },
    userConnection: { type: Schema.Types.ObjectId, ref: 'User' },
    userConnectionStatus: {
        type: String,
        enum: ['following', 'blocked', 'requested']
    }
}

Eg User: me, userConnection: 'someone', userConnectionStatus: 'following' 例如,用户:我,userConnection:“某人”,userConnectionStatus:“正在关注”

What I am trying to achive is to return 2 more fields, 1. My userConnectionStatus to him 2. His userConnectionStatus to me 我要达到的目标是再返回2个字段:1.我的userConnectionStatus给他2.他的userConnectionStatus给我

And not to return users who have blocked me. 而不是返回阻止我的用户。 What is the best approach when it comes to this DB structure. 关于此数据库结构,最好的方法是什么。

Thank you for your time 感谢您的时间

Preventing blocked users was solved by selecting all blocked users, and adding $nin in match inside aggregate. 通过选择所有被阻止的用户并在聚合中的match中添加$ nin来解决防止被阻止的用户的问题。

For connection status, I have resolved the problem by adding 2 virtual fields to User. 对于连接状态,我已通过向用户添加2个虚拟字段解决了该问题。

UserMongoSchema.virtual('userConnectionStatus', {
  ref: 'UserConnection',
  localField: '_id',
  foreignField: 'user',
  justOne: true
});
UserMongoSchema.virtual('connectionStatus', {
  ref: 'UserConnection',
  localField: '_id',
  foreignField: 'userConnection',
  justOne: true
});

And populating them on results 并根据结果填充它们

...
.exec((errAgg, results) => {
      User.populate(results[0].data, [ 
            { path: 'userConnectionStatus', match: { userConnection: req.userCode }, select: 'userConnectionStatus' },
            { path: 'connectionStatus', match: { user: req.userCode }, select: 'userConnectionStatus' },
        ], (errPop, populateResponse) => {
            if (errPop) { return next(errPop); }

            populateResponse = populateResponse.map((row) => {
                row['userConnectionStatus'] = row.userConnectionStatus ? row.userConnectionStatus.userConnectionStatus : null;
                row['connectionStatus'] = row.connectionStatus ? row.connectionStatus.userConnectionStatus : null;
                return row;
            });
...

Looking at the order of actions, I think this won't affect performance since I am running populate only on those matched top X (max 100) results. 查看操作顺序,我认为这不会影响性能,因为我只在匹配的前X个(最多100个)结果上填充。

I won't mark this as Answer yet. 我不会将其标记为“答案”。 If you have any opinion about if this is bad practice or if there is a better way of doing it, feel free to comment. 如果您对这是不好的做法还是有更好的方法有任何意见,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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