我有以下 Room 对象结构。
type Room struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Title string `json:"title" bson:"title"`
Description string `json:"description" bson:"description,omitempty"`
Type string `json:"type" bson:"type,omitempty"`
AdminId bson.ObjectId `json:"admin_id" bson:"admin_id"`
CreatedOn time.Time `json:"created_on" bson:"created_on"`
Messages []Message `json:"messages" bson:"messages,omitempty"`}
其中 Messages 是具有以下结构的嵌套对象数组
type Message struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Text string `json:"text" bson:"text"`
Author Author `json:"author" bson:"author"`
CreatedOn time.Time `json:"createdon" bson:"created_on"`
Reply []Message `json:"reply" bson:"reply,omitempty"`}
我想通过房间集合中的消息执行搜索查询。我尝试使用 "$in" 但没有帮助我。
此外,我必须通过匹配值来搜索元素。我可以使用 bson 正则表达式来做到这一点。
&bson.RegEx{Pattern: textToFind, Options: "i"}
总而言之,我需要通过 Room 文档中嵌套对象中的 Text 字段搜索消息。
附言抱歉可能有错误,英语不是我的母语。
更新
基本上,我想找到给定房间中包含某些子字符串的所有消息。例如,搜索房间(聊天)“A”中包含“some text”子字符串的所有消息。
最佳答案
您可以尝试下面的 mongo shell 聚合管道。
$match一些房间属性(例如 _id)。
$unwind 消息(将 messages 数组转换为对象)在房间里。
$matches 在输入正则表达式上针对 text 字段过滤 messages。
$group将消息对象返回到 messages 数组中。
$project 排除 _id 并仅包含 messages 用于输出。
db.collection.aggregate(
{$match:{"_id":roomid}},
{$unwind:"$messages"},
{$match:{"messages.text": { $regex: /textToFind/i } }},
{$group:{_id:null,messages:{$push:"$messages"}}},
{$project:{_id:0, messages:1}})
以下是未经测试的 mgo 等价物。
match1 := bson.M{
"$match": bson.M{
"_id": roomid,
},
}
unwind := bson.M{
"$unwind": "$messages",
}
match2 := bson.M{
"$match": bson.M{"messages.text": &bson.RegEx{Pattern: textToFind, Options: "i"}},
}
group := bson.M{
"$group": bson.M{
"_id": null,
"messages": bson.M{
"$push": "$messages",
},
},
}
project := bson.M{
"$project": bson.M{
"_id": 0,
"messages":1,
},
}
all := []bson.M{match1, unwind, match2, group, project}
pipe := collection.Pipe(all)
result := []bson.M{}
err := pipe.All(&result)
关于mongodb - 按给定字段搜索嵌套对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016813/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格: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[
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢