以某种方式对记录进行排序,以便仅匹配ID的记录排在首位? [英] Sorting records in a way so that only records which matches an id comes first?

查看:32
本文介绍了以某种方式对记录进行排序,以便仅匹配ID的记录排在首位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,可以向我返回某些情况下的记录或消息:

I have a query which return me the records or messages for a certain scenario:

const [messages, messageCount] = await Promise.all([
    MessageModel.find(params).select(filterObject).limit(ctx.query.limit).skip(ctx.paginate.skip)
      .sort('-sex -age')
      .lean(),
    MessageModel.countDocuments(params),
  ]);

有什么方法可以先获取与对象ID匹配的记录,然后再获取其余的记录?

Is there any way to get the records first which matches an object id, and then rest of the records?

结果

{
_id:abc0aa8573bfa917b152cdbc
isPrivate:false
message:"My name is stark"
gender:"unisex"
age: "19"
createdBy:abcff9ef71fa048cea3c8a97
}
{
_id:abc0aa8573bfa917b152cdbc
isPrivate:false
message:"My name is james"
gender:"unisex"
age: "20"
createdBy:defff9ef71fa048cea3c8a97
}
{
_id:abc0aa8573bfa917b152cdbc
isPrivate:false
message:"My name is harry"
gender:"unisex"
age: "20"
createdBy:defff9ef71fa048cea3c8a97
}
{
_id:abc0aa8573bfa917b152cdbc
isPrivate:false
message:"My name is max"
gender:"unisex"
age: "20"
createdBy:abcff9ef71fa048cea3c8a97
}

现在,我首先要由abc创建的那些消息.所有与特定ID匹配的记录都将排在第一位,然后其余记录保持不变

Now I want those messages that was created by the abc... first. All those records that match a certain id would come first and then rest of the records would remain the same

{
_id:abc0aa8573bfa917b152cdbc
isPrivate:false
message:"My name is stark"
gender:"unisex"
age: "19"
createdBy:abcff9ef71fa048cea3c8a97
}

{
    _id:abc0aa8573bfa917b152cdbc
    isPrivate:false
    message:"My name is max"
    gender:"unisex"
    age: "20"
    createdBy:abcff9ef71fa048cea3c8a97
    }
    {
    _id:abc0aa8573bfa917b152cdbc
    isPrivate:false
    message:"My name is james"
    gender:"unisex"
    age: "20"
    createdBy:defff9ef71fa048cea3c8a97
    }
    {
    _id:abc0aa8573bfa917b152cdbc
    isPrivate:false
    message:"My name is harry"
    gender:"unisex"
    age: "20"
    createdBy:defff9ef71fa048cea3c8a97
    }
    

推荐答案

我认为没有任何直接的方法可以执行此操作,如果您确实想尝试使用 $ cond 运算符进行检查特定领域的条件,

I don't think there is any straight way to do this, If you really want to then try $cond operator to check condition on specific field,

  • $ project 中添加新字段 matchResult ,它将检查 createdBy 是否匹配,返回1,否则返回0,
  • $ sort matchResult 降序
  • add new field matchResult in $project it will check createdBy is matching return 1 otherwise 0,
  • $sort by matchResult in descending order
const [messages, messageCount] = await Promise.all([
    MessageModel.aggregate([
        { $match: params },
        {
            $project: {
                ...filterObject,
                matchResult: {
                    $cond: [
                      { $eq: ["$createdBy", ObjectId("abcff9ef71fa048cea3c8a97")] },
                      1,
                      0
                    ]
                }
            }
        },
        { $sort: { matchResult: -1 } },
        { $skip: ctx.paginate.skip },
        { $limit: ctx.query.limit }
    ]),
    MessageModel.countDocuments(params),
]);

游乐场

第二个选项是 $ regexMatch 聚合运算符从MongoDB v4.2开始,

Second option is $regexMatch aggregation operator starting from MongoDB v4.2,

游乐场

我不建议对收集中的大数据使用这种方法,这可能会导致性能问题,最好在查询后在客户端进行排序.

I am not recommending this approach for big data in collection, this may cause performance issue, it is better to sort in your client side after query.

这篇关于以某种方式对记录进行排序,以便仅匹配ID的记录排在首位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆