在我的 offer 集合中,我有这样的文档:
{
"_id" : ObjectId("535419d2d6ad229f217359af"),
"application" : "app1",
"category_id" : "nord outdoor",
"category_name" : "nord outdoor",
"id" : "810928",
"name" : "offer name",
"ref_code" : "810928",
"srcs" : {
"src1" : {
"price" : 149.9,
"updated_at" : ISODate("2014-04-20T19:21:15Z"),
"url" : "http://www.offer.com/810928?src=1"
},
"src2" : {
"price" : 149.9,
"updated_at" : ISODate("2014-04-20T20:21:15Z"),
"url" : "http://www.offer.com/810928?src=2"
}
},
"updated_at" : ISODate("2014-04-20T19:21:15Z")
}
我需要运行以下查询(使用Ruby Moped):
app = 'app1'
tolerance = Time.now - 3600
srcs = %w(src1 src2 srcN)
result = []
srcs.each do |src|
query = collection.find({:application => app, :"srcs.#{src}.updated_at" => {:$lt => tolerance}})
query = query.sort(:"srcs.#{src}.updated_at" => 1)
result << query.limit(512).entries
end
如何创建索引来执行此查询,请记住srcs
可以假定任何值?