我在实体中有这样的字段:
private String userId;
private String username;
private Date created;
private List<Comment> comments = new ArrayList<>();
Comment 有这样的字段:
private String userId;
private String username;
private String message;
private Date created;
我需要进行聚合,并收到类似这样的信息:
{
"userId" : "%some_userId%",
"date" : "%some_date%",
"commentsQty" : 100,
"isCommented" : true
}
我的聚合看起来像这样:
{ "aggregate" : "%entityName%" , "pipeline" : [
{ "$project" : {
"username" : 1 ,
"userId" : 1 ,
"created" : 1 ,
"commentQty" : { "$size" : [ "$comments"]}}}]}
而且它工作正常。但我还需要检查,如果评论数组包含一些评论,具有特定的 userId。我试过这个,但它无法执行:
{ "aggregate" : "%entityName%" , "pipeline" : [
{ "$project" : {
"username" : 1 ,
"userId" : 1 ,
"created" : 1 ,
"commentQty" : { "$size" : [ "$comments"]} ,
"isCommented" : { "$exists" : [ "$comments.userId" , "5475b1e45409e07b0da09235"]}}}]}
带有这样的消息:命令执行失败:错误 [异常:无效的运算符'$exists']
如何进行这样的检查?
UPD:还尝试了运算符 $in 和类似运算符,但它们对查询有效,对聚合无效。
最佳答案
with such message : Command execution failed: Error [exception: invalid operator '$exists']
当前 $exists运算符在 aggregation 管道中不可用。
编辑:
写出更好的答案:
您可以通过以下方式检查是否有任何用户发表评论:
$setIntersection如果用户真的对帖子发表了评论,运算符会获取包含我们正在寻找的 userId 的数组。$size运算符以获取结果数组的大小。$gt运算符检查大小是否大于 0。userId的评论,否则不存在。示例代码:
var userIdToQuery = "2";
var userIdsToMatchAgainstComments = [ObjectId("5475b1e45409e07b0da09235")];
db.t.aggregate([
{$match:{"userId":userIdToQuery}},
{$project:{"userName":1,
"created":1,
"commentQty":{$size:"$comments"},
"isCommented":{$cond:
[{$gt:[
{$size:
{$setIntersection:["$comments.userId",
userIdsToMatchAgainstComments]}}
,0]},
true,false]}}}
])
上一个答案:
Unwind 评论。Project 一个额外的字段 isCommented 对于每个评论文档,
检查它是否有我们正在搜索的 userId,如果它有
相应的userId,然后将变量设置为1,否则为0。Group 再次将文档组合在一起,对 isCommented 中的值求和,如果
它是 > 0,具有用户 id 的文档存在于 else 组中
不是。相应地投影字段。代码:
{ "aggregate" : "%entityName%" , "pipeline" :[
{$unwind:"$comments"},
{$project:{
"username":1,
"userId":1,
"created":1,
"comments":1,
"isCommented":{$cond:[{$eq:["$comments.userId",
ObjectId("5475b1e45409e07b0da09235")
]
},
1,
0]}}},
{$group:{"_id":{"_id":"$_id",
"username":"$username",
"userId":"$userId",
"created":"$created"},
"isCommented":{$sum:"$isCommented"},
"commentsArr":{$push:"$comments"}}},
{$project:{"comments":"$commentsArr",
"_id":0,
"username":"$_id.username",
"userId":"$_id.userId",
"created":"$_id.userId",
"isCommented":{$cond:[{$eq:["$isCommented",0]},
false,
true]},
"commentQty":{$size:"$commentsArr"}}},
]}
关于MongoDB 聚合。检查嵌套数组是否包含值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27153948/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)