【问题标题】:How to sort 2 groups of data in MongoDB?如何在 MongoDB 中对 2 组数据进行排序?
【发布时间】:2015-03-27 10:41:19
【问题描述】:

我有一个主题集合,我存储这样的主题:

{
    "headline" : "Quis sint et aut non quos.",
    "announce" : "Rerum suscipit omnis qui ducimus.",
    "body" : "<p>Ad amet et. Sed quod porro maxime doloribus.</p>",
    "published_at" : ISODate("2015-02-20T21:24:52.000Z"),
    "rubrics" : [ 
        {
            "slug" : "news",
            "title" : "News"
        }
    ],
    "subrubrics" : [ 
        {
            "slug" : "ratings",
            "title" : "Ratings"
        }
    ],
    "lists" : [ 
        {
            "title" : "News",
            "slug" : "news",
            "position": 12
        }
    ]
}

我的目标是按如下方式排序主题:

  1. 第一部分:所有带有lists.slug = "A"的主题按lists.position排序
  2. 第二部分:先行,所有rubrics.slug or subrubrics.slug = "A"的主题按published_at排序

预期输出:

[
  /* First part */
  {"lists": [ { "title": "News", "slug": "news", "position": 1 } ]},
  {"lists": [ { "title": "News", "slug": "news", "position": 2 } ]},
  {"lists": [ { "title": "News", "slug": "news", "position": 3 } ]},
  {"lists": [ { "title": "News", "slug": "news", "position": 4 } ]},
  {"lists": [ { "title": "News", "slug": "news", "position": 5 } ]},
  {"lists": [ { "title": "News", "slug": "news", "position": 6 } ]},
  /* Second part */
  {"published_at": ISODate("2015-02-20T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
  {"published_at": ISODate("2015-02-19T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
  {"published_at": ISODate("2015-02-18T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
  {"published_at": ISODate("2015-02-17T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
  {"published_at": ISODate("2015-02-16T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
  {"published_at": ISODate("2015-02-15T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
  {"published_at": ISODate("2015-02-13T00:00:00.000Z"), "rubrics": [ { "title": "News Rubric", "slug": "news" } ]},
]

请注意,如果我会提前获得 rubric 或 subrubric 的 slug,我现在不会。

我对 MongoDB 聚合框架不是很有经验,我只能写这样的东西,它只做第一部分(MongoID):

projection = {
    _id: 0,
    lists: 1,
}
slug = 'news'
collection.aggregate(
    {'$project' => projection},
    {'$unwind' => '$lists'},
    {'$match' => {'lists.slug' => slug}},
    {'$sort' => {'lists.position' => 1}}
)

如何在一个查询中完成这两个部分?会有效吗?是否可以通过 Mongoid 做到这一点?

【问题讨论】:

  • 你能在这里发布你的预期输出吗?
  • @yogesh,有问题做到了
  • lists 是一个数组。您希望如何对具有两个 lists.position 值 1 和 12 的文档相对于另一个具有 lists.position 值 4 和 8 的文档进行排序?假设两个文档都有一个值lists.slug = "A"
  • lists 数组肯定不能有具有相同 slug 的对象。我使用上面写的unwind 运算符对具有不同lists.position 值的文档进行排序。
  • 深入了解预期的输出。那将需要两个预测。虽然使用逻辑或很容易在一个查询中查询不同的条件集,但查询的输出只能有一个投影。

标签: ruby-on-rails mongodb mongoid aggregation-framework


【解决方案1】:

我在看documentation for mongodb for sort,这看起来真的很简单

有一个 $sort 方法,该方法对所有输入文档进行排序,并按排序顺序将它们返回到管道。

示例

{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

现在 取 2 个可能值中的 1 个

  • 1 指定升序。
  • -1 指定降序。

你的例子

collection.aggregate(
    {'$project' => projection},
    {'$unwind' => '$lists'},
    {'$match' => {'lists.slug' => slug}},
    {'$sort' => {'lists.position' => 1, 'lists.slug' => -1}}
)

{'$sort' =&gt; {'lists.position' =&gt; 1, 'lists.slug' =&gt; -1}}这一行

请在mongodb手册http://docs.mongodb.org/manual/reference/operator/aggregation/sort/中阅读更多关于如何排序的内容@

【讨论】:

  • 这不包括第二部分。在第二部分中,我需要获取不在当前列表中的所有项目,并按发布日期对其进行排序。
猜你喜欢
  • 1970-01-01
  • 2014-07-14
  • 2019-05-30
  • 2020-02-06
  • 2019-01-02
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多