我的数据库中有 > 8000 条记录,这是其中之一:
{
"_id" : ObjectId("57599c498c39598eafb781b9"),
"_class" : "vn.cdt.entity.db.AccessLog",
"url" : "/shop/huenguyenshop/browse",
"ip" : "10.0.0.238",
"sessionId" : "86E5CF8E6D465A6EDFE7C9BF7890AA4B",
"oldSessionId" : "86E5CF8E6D465A6EDFE7C9BF7890AA4B",
"cookie" : "{\"sessionId\":\"86E5CF8E6D465A6EDFE7C9BF7890AA4B\",\"objects\":[{\"id\":\"903815555908\",\"type\":\"VIEW_SHOP\",\"count\":1}]}",
"isCookie" : true,
"createTime" : NumberLong(1464935913641),
"objectId" : "903815555908",
"type" : "VIEW_SHOP"
}
我想做什么:
我想找到所有具有相同 oldSessionId 的记录(type: VIEW_ITEM or type: BUY_ITEM)并且 createTime 是最新的。
我尝试过的:
pipeline = ([
{"$group" : { "_id": "$oldSessionId", "count": { "$sum": 1 } }},
{"$match": {"count" : {"$gt": 1} } },
{"$project": {"oldSessionId" : "$_id", "_id" : 0} }
])
但是那个管道只给我sessionId
find({'createTime': {'$lt':1464419127000, '$gt':1464332727000},
'$or':[{'type':'BUY_ITEM'},{'type':'VIEW_ITEM'}]})
那个find给我在特定时间type:VIEW_ITEM或type:BUY_ITEM的所有记录。
我不知道如何使用 type 和 createTime 添加过滤器以获得我想要的。
更新 感谢@chridam 帮助我:
如果我想将特定日期添加到聚合中,我可以像这样进行添加查询:
pipeline = \
(
[
{ "$match": {
"createTime": {"$lt":1464419127000, "$gt":1464332727000 },
"type": { "$in": ["VIEW_ITEM", "BUY_ITEM"] }
}
},
{ "$sort": { "createTime": -1, "oldSessionId": 1 } },
{
"$group":
{ "_id": "$oldSessionId",
"_class": { "$first": "$_class" },
"url": { "$first": "$url" },
"ip": { "$first": "$ip" },
"sessionId": { "$first": "$sessionId" },
"oldSessionId": { "$first": "$oldSessionId" },
"cookie": { "$first": "$cookie" },
"isCookie": { "$first": "$isCookie" },
"createTime": { "$first": "$createTime" },
"objectId": { "$first": "$objectId" },
"type": { "$first": "$type" },
}
}
]
)
最佳答案
要获取具有相同 oldSessionId 的所有文档(类型:VIEW_ITEM 或类型:BUY_ITEM)并且 createTime 是最新的,您需要进行具有以下参与者(阶段)的聚合管道展示:
$match 阶段:
VIEW_ITEM 或 BUY_ITEM 的文档。您可以使用 $in 运算符与查询,因为它允许您选择 type 字段的值等于指定数组中的任何值的文档,该数组恰好是具有两个可能类型值的列表即 ["VIEW_ITEM", "BUY_ITEM"]。 $sort 阶段
createTime 字段上聚合这些过滤后的文档。 $group 阶段
oldSessionId 键对所有有序文档进行分组,使用 $first 添加您想要的字段 运算符(operator)。将上述所有管道拼凑在一起形成以下聚合管道:
pipeline = [
{ "$match": { "type": { "$in": ["VIEW_ITEM", "BUY_ITEM"] } } },
{ "$sort": { "createTime": -1, "oldSessionId": 1 } },
{
"$group": {
"_id": "$oldSessionId",
"_class": { "$first": "$_class" },
"url": { "$first": "$url" },
"ip": { "$first": "$ip" },
"sessionId": { "$first": "$sessionId" },
"cookie": { "$first": "$cookie" },
"isCookie": { "$first": "$isCookie" },
"createTime": { "$first": "$createTime" },
"objectId": { "$first": "$objectId" },
"type": { "$first": "$type" },
}
}
]
关于MongoDB、PyMongo - 根据查找条件聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958808/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS
我需要根据字符串路径的长度将字符串路径数组转换为符号、哈希和数组的数组给定以下数组:array=["info","services","about/company","about/history/part1","about/history/part2"]我想生成以下输出,对不同级别进行分组,根据级别的结构混合使用符号和对象。产生以下输出:[:info,:services,about:[:company,history:[:part1,:part2]]]#altsyntax[:info,:services,{:about=>[:company,{:history=>[:part1,:pa
除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p
我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr