【问题标题】:MongoDB - Sort by the maxnium number of keywords in inputMongoDB - 按输入中的最大关键字数排序
【发布时间】:2017-12-08 00:33:44
【问题描述】:

我正在寻找构建一个 mongo 查询,该查询将按输入数组中指定的重复关键字的数量对文档进行排序。我的文档如下所示

{
    "_id" : 1,
    "name" : "invoice",
    "rules" : [ 
        {
            "_any" : [ 
                "invoice", 
                "invoices", 
                "total", 
                "order", 
                "bill"
            ]
        }
    ]
}
{
    "_id" : 2,
    "name" : "Terms of Service",
    "rules" : [ 
        {
            "_any" : [ 
                "rule", 
                "rules", 
                "terms"
            ]
        }
    ]
}

那么,假设我的输入数组包含以下关键字

[ 'invoice',
  'invoice',
  'order',
  'bill',
  'bill',
  'bill',
  'bill',
  'bill',
  'terms'
 ]

Mongo 应该首先显示 _id: 1 然后是 _id: 2 的结果,因为输入数组包含属于 _id: 1 的关键字的最大数量

提前致谢

【问题讨论】:

    标签: arrays mongodb aggregation-framework


    【解决方案1】:

    您可以将索引放入数组中。 0:“发票”, 1:“账单”, 2:“订单”, 3:“发票”

    那么使用 array.length 就可以很容易地把它按正确的顺序排列。

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 2020-10-22
      • 1970-01-01
      • 2011-08-28
      • 2013-05-29
      • 2017-08-20
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      相关资源
      最近更新 更多