2012-07-25 88 views
1

我试图用minute大于12如何使用MongoDB中的聚合框架执行此查询?

我的数据结构返回visits看起来是这样的:

{ "_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比12 minute更大的回报?任何帮助,将不胜感激。

+0

难道你不想让$放开字段$访问(什么是$ data?)。我认为$ unwind只适用于数组(你的访问不是)。 – Thilo 2012-07-25 01:15:24

+0

查看我的编辑,在'$ unwind'上。仍然是'[]'的结果。其次,我应该改变我的数据结构,以便它使用数组而不是散列?有什么办法可以使用哈希来代替数组?为什么我使用哈希值没有特别的理由。只是个人喜好 – 2012-07-25 01:25:39

+0

你有没有考虑过使用单独的收集访问日志?这会使这个特定的查询变得微不足道(当然,我们不知道你的其他查询)。 – Thilo 2012-07-25 01:27:36

回答

3

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

一旦$ unwind将您的访问数组元素分割成它自己的文档,您可以在管道的末尾添加{$ match:{“visits.minute”:{$ gt:}}},您想要的访问。