1

我正在尝试返回大于 12visits的值。minute

我的数据结构如下所示:

{ "_id" : "20120723/foobar/song/custom-cred",
  "live_daily_count" : 4,
  "metacontent" : { "date" : "20120723",
    "live_daily_statable_slug" : "custom-cred",
    "live_daily_statable_title" : "custom cred",
    "live_daily_statable_type" : "Song",
    "url" : "foobar/songs/custom-cred",
    "user_slug" : "foobar" },
  "visits" : [
    { "country_name" : "UK",
      "iso_two_letter_country_code" : "UK",
      "referer" : "http://localhost:3000/foobar/songs/no-title-with-space",
      "minute" : 12,
      "token_id" : "134300236111rcbmbmvv" },
    { "country_name" : "UK",
      "iso_two_letter_country_code" : "UK",
      "referer" : "http://localhost:3000/foobar/songs/no-title-with-space",
      "minute" : 13,
      "token_id" : "134300242111pjvkjjkf" },
    { "country_name" : "UK",
      "iso_two_letter_country_code" : "UK",
      "referer" : "http://localhost:3000/foobar/songs/no-title-with-space",
      "minute" : 13,
      "token_id" : "134300243511udbnqldm" }
  ]
}

我正在使用 Mongodb Ruby 驱动程序。我尝试了以下方法:

conn = Mongo::Connection.new
db   = conn['foobar_development']
cmd = {
  aggregate: 'live_daily_stats',
  pipeline: [
    { '$match' => { :_id => "20120723/foobar/song/custom-cred" } },
    { '$project' => {
      :visits => 1,
    } },
    { '$unwind' => '$visits' },
    # { '$group' => {
    #   :_id => '$_id'
    # } },
  ]
}

res = db.command(cmd)['result']

它现在返回:

[
    [0] {
           "_id" => "20120723/foobar/song/custom-cred",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                               "referer" => "http://localhost:3000/foobar/songs/custom-cred",
                                                "minute" => 12,
                                              "token_id" => "134300236111rcbmbmvv"
        }
    },
    [1] {
           "_id" => "20120723/foobar/song/custom-cred",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                        "follower_class" => "non_follower",
                                               "referer" => "http://localhost:3000/foobar/songs/custom-cred",
                                                "minute" => 13,
                                              "token_id" => "134300242111pjvkjjkf"
        }
    },
    [2] {
           "_id" => "20120723/foobar/song/custom-cred",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                        "follower_class" => "non_follower",
                                               "referer" => "http://localhost:3000/foobar/songs/custom-cred",
                                                "minute" => 13,
                                              "token_id" => "134300243511udbnqldm"
        }
    }
]

如何确保结果只返回visits大于minute12 的?任何帮助,将不胜感激。

4

1 回答 1

3

您在开始时使用的 $match 管道元素可以多次使用,包括在您的 $unwind 步骤之后。

一旦 $unwind 将您的访问数组元素每个拆分到自己的文档中,您就可以将 {$match:{"visits.minute":{$gt:12}}} 添加到管道的末尾,这应该只留下您想要的访问。

于 2012-07-25T06:46:26.980 回答