原始MongoDB和Mongoid之间的结果不同

时间:2015-02-18 18:09:40

标签: mongodb mongoid

我直接在MongoDB中运行此查询:

db.events.aggregate([
  {$match: {name: 'new_session', created_at: {
    $gte: ISODate('2010-01-01T00:00:00.000Z'),
    $lt: ISODate('2015-05-01T00:00:00.000Z')
  }}},
  {"$project": {
  "user_id": "$user_id",
  "year": {"$year": "$created_at"}, 
  "month": {"$month": "$created_at"},
  "day": {"$dayOfMonth": "$created_at"}
}},
{"$group": {
     "_id": {"year": "$year", "month": "$month", "day": "$day"},
     "uniques": {"$addToSet": "$user_id"},
     "total_sessions": {"$sum": 1}
}}
])

它会返回我的期望。

使用Mongoid模型运行下一个查询会返回不同的结果:

results = Event.collection.aggregate([
  {"$match" => {"name" => 'new_session', "created_at" => {
    "$gte" => @start_time,
    "$lt" => @end_time
  }}},
  {"$project" => {
    "user_id" => "$user_id",
    "year" => {"$year" => "$created_at"}, 
    "month" => {"$month" => "$created_at"},
    "day" => {"$dayOfMonth" => "$created_at"}
  }},
  {"$group" => {
       "_id" => {"year" => "$year", "month" => "$month", "day" => "$day"},
       "uniques" => {"$addToSet" => "$user_id"},
       "total_sessions" => {"$sum" => 1}
  }}
])

任何想法为何与众不同? Mongoid在幕后做了一些魔术吗?我没有启用缓存。我检查过它是否连接到正确的数据库。一切似乎都很好。

有什么建议吗?

由于

0 个答案:

没有答案