MongoDB在两个字段上的自定义排序 [英] MongoDB Custom sorting on two fields

查看:99
本文介绍了MongoDB在两个字段上的自定义排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的MongoDB文档中,我们有两个字段, organisationId employeeId .

In our MongoDB document, we have two fields, organisationId and employeeId.

我想显示集合中所有具有与查询参数匹配的元素,因此是基本OR.

I want to show all the elements in a collection that have either of them matching the query parameters, so a basic OR.

排序的一个条件是我必须首先出现两个字段都与查询参数匹配的文档,然后才出现与 organizationName 参数匹配的文档,下一个.

One condition for Sorting is that I require is that the documents that have both the fields matching the query parameters should occur first, and then the documents matching organisationName parameter, next.

该想法是先显示员工(即您)的数据,然后再显示您组织的数据. (在我们的案例中,您首先提出的主题是您提出的,然后是组织中其他员工提出的主题. 到目前为止,我正在使用以下查询来实现这一目标-

The idea is to show the data for the employee (i.e., you) first and then that of your organisation. (In our case topics suggested by you first and then by other employees in your organisation. As of now, I am using the following query to achieve this -

Campaigns.find({$and : [{'organisationName' : organisationName},{'employeeName' : userName}]},{}),
Campaigns.find({$and : [{'organisationName' : organisationName},{'employeeName' : {$ne : userName}}]},{}) 

但这对我来说似乎不是最有效的方法.只需一个调用就可以执行此操作的任何其他查询都将非常好,因为这也将有助于分页.

But this does not seem like the most effective way to me. Any other query that can do this in just one call would be very nice, as that will help in the pagination too.

预先感谢

推荐答案

此聚合查询获得所需的结果:

This Aggregation query gets the desired result:

输入文档:

{ "org" : "o1", "emp" : "e1", "data" : "1234" }
{ "org" : "o1", "emp" : "e2", "data" : "abcd" }
{ "org" : "o1", "emp" : "b3", "data" : "xyz" }
{ "org" : "o2", "emp" : "z3", "data" : "zzz" }

查询参数:

orgNameParam = "o1"
usrNameParam = "e2"

查询:

db.orgemp.aggregate([
  { $match: { org: orgNameParam} },
  { $facet: {
     firstQuery: [
          { $match: { emp: usrNameParam } }
     ],
     secondQuery: [
          { $addFields: { isNotEmp: { $ne: [ "$emp", usrNameParam ] } } },
          { $match: { isNotEmp: true } },
          { $project: { isNotEmp: 0 } },
          { $sort: { emp: 1 } },
     ],
  } },
  { $project: { result: { $concatArrays: [ "$firstQuery", "$secondQuery" ] } } },
])

结果:

{
        "result" : [
                {
                        "_id" : ObjectId("5dc51432c2ac920e04692778"),
                        "org" : "o1",
                        "emp" : "e2",
                        "data" : "abcd"
                },
                {
                        "_id" : ObjectId("5dc51432c2ac920e04692779"),
                        "org" : "o1",
                        "emp" : "b3",
                        "data" : "xyz"
                },
                {
                        "_id" : ObjectId("5dc51432c2ac920e04692777"),
                        "org" : "o1",
                        "emp" : "e1",
                        "data" : "1234"
                }
        ]
}

这篇关于MongoDB在两个字段上的自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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