我有以下一系列书籍。我正在尝试投影数组中元素的单个属性。
我的查询正在为 author A1 的 year 2012 的 book 投影文本属性。
书籍数组示例:
[
{
book_id: "1",
title:'B1',
year: 2012,
comments: [
{
author: "A1",
text: "C1"
}
]
},
{
book_id: "2",
title:'B2',
year: 2012,
comments: [
{
author: "A2",
text: "C2.0"
},
{
author: "A1",
text: "C2.1"
},
{
author: "A1",
text: "C2.2"
},
]
},
{
book_id: "3",
title:'B3',
year: 2013,
comments: [
{
author: "A1",
text: "C3"
}
]
},
]
预期结果
[
"C1",
"C2.1",
"C2.2"
]
我尝试了以下方法:
// query using find
db.books.find(
{year:2012, comments: {$elemMatch : {author:"A1" } } },{_id:0,'comments.text':1}
)
// results
/* 1 */
{
"comments" : [
{
"text" : "C1"
}
]
}
/* 2 */
{
"comments" : [
{
"text" : "C2.0"
},
{
"text" : "C2.1"
},
{
"text" : "C2.2"
}
]
}
// query using aggregation
db.books.aggregate(
[
{$match: {year:2012}},
{
$project:{
comments: {
$filter:{
input: "$comments",
as:'comment',
cond: {$eq:['$$comment.author','A1']}
}
}
}
}
])
// results in
/* 1 */
{
"_id" : ObjectId("5d3446f9d9eac0ed968aad4b"),
"comments" : [
{
"author" : "A1",
"text" : "C1"
}
]
}
/* 2 */
{
"_id" : ObjectId("5d3446f9d9eac0ed968aad4c"),
"comments" : [
{
"author" : "A1",
"text" : "C2.1"
},
{
"author" : "A1",
"text" : "C2.2"
}
]
}
最佳答案
您可以使用以下聚合
db.collection.aggregate([
{ "$match": { "year": 2012, "comments": { "$elemMatch": { "author": "A1" }}}},
{ "$unwind": "$comments" },
{ "$match": { "year": 2012, "comments.author": "A1" }},
{ "$group": {
"_id": null,
"data": { "$push": "$comments.text" }
}}
])
[
{
"_id": null,
"data": [
"C1",
"C2.1",
"C2.2"
]
}
]
您不能使用 mongodb 返回简单的数组字符串,因为它只返回 BSON 文档而不仅仅是元素数组。
关于MongoDb:在有条件的文档中将元素的属性投影到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57132422/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我有多个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]
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我正在使用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
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat