我在 mongoDB 中有这个模式文档:
{
"_id": UUID("cf397865-c000-4f51-8959-1aae84769706"),
"CreationDateTime": ISODate("2016-05-06T05:09:14.589Z"),
"WKT": "",
"Distributions": [{
"_id": UUID("bb95bedb-4baa-4ada-90b1-0d763e70ebfe"),
"DeliveryType": 1,
"DistributionData": [{
"Key": "Topic",
"Value": "Topics",
"Children": null
}, {
"Key": null,
"Value": null,
"Children": null
}, {
"Key": "Message",
"Value": "test",
"Children": null
}
],
"Schedules": [
ISODate("2016-05-06T05:09:56.988Z")
]
}
],
}
我想清理我的数据库。所以为了我决定删除 DistributionData 中的空对象。我如何删除所有三个属性都具有空值的对象:
{
"Key": null,
"Value": null,
"Children": null
}.
我写了这个查询:
db.Events.update(
{},
{$pull:
{
results: {
$elemMatch: {
"Distributions[0].DistributionData" : {$in:[null]}
}
}
}
},
{ multi: true }
)
当我执行这个查询时,没有任何反应!我知道 $elemMatch 是错误的..
现在我如何删除 DistributionData 中所有字段为空的 json 对象?
我读了 this 和 this 但让我感到困惑......
Edit
我写了这个查询:
db.Events.update(
{},
{$pull:
{
Distributions : {
DistributionData:{
$elemMatch: {
"Key" : null
}
}
}
}
},
{ multi: true }
)
此查询将完全删除 DistributionData 数组中包含空键对象的 Distribution 中的对象:
结果:
{
"_id": UUID("cf397865-c000-4f51-8959-1aae84769706"),
"CreationDateTime": ISODate("2016-05-06T05:09:14.589Z"),
"WKT" : "",
"Distributions" : [],...
最佳答案
您可以 $pull 从“外部数组”中删除“所有内部元素”的“第一个匹配项”只需执行以下操作:
db.Events.updateMany(
{
"Distributions.DistributionData": {
"$elemMatch": {
"Key": null,
"Value": null,
"Children": null
}
}
},
{
"$pull": {
"Distributions.$.DistributionData": {
"Key": null,
"Value": null,
"Children": null
}
}
}
)
如果您在 "Distributions" 中只有一个条目,那很好数组或至少其中一个条目具有符合条件的子数组条目。 positional $ operator 是这样的适用于所有版本的 MongoDB。
如果数据在“外部”中有“多个”匹配 "Distributions" array 然后如果你有 MongoDB 3.6 你可以应用 positional filtered $[<identifier>] operator修改所有匹配的条目:
db.Events.updateMany(
{
"Distributions.DistributionData": {
"$elemMatch": {
"Key": null,
"Value": null,
"Children": null
}
}
},
{
"$pull": {
"Distributions.$[element].DistributionData": {
"Key": null,
"Value": null,
"Children": null
}
}
},
{
"arrayFilters": [
{ "element.DistributionData": {
"$elemMatch": {
"Key": null,
"Value": null,
"Children": null
}
}}
]
}
)
在这种情况下 arrayFilters选项定义了一个条件,我们通过该条件匹配“外部”数组中的条目,以便这实际上可以应用于匹配的所有内容。
或者确实自 $pull 本质上本身就有这些条件,那么你可以交替使用 positional all $[] 本例中的运算符:
db.Event.updateMany(
{
"Distributions.DistributionData": {
"$elemMatch": {
"Key": null,
"Value": null,
"Children": null
}
}
},
{
"$pull": {
"Distributions.$[].DistributionData": {
"Key": null,
"Value": null,
"Children": null
}
}
}
)
这两种情况都通过删除所有 null 的内部项目来更改问题中的文档。键:
{
"_id" : UUID("cf397865-c000-4f51-8959-1aae84769706"),
"CreationDateTime" : ISODate("2016-05-06T05:09:14.589Z"),
"WKT" : "",
"Distributions" : [
{
"_id" : UUID("bb95bedb-4baa-4ada-90b1-0d763e70ebfe"),
"DeliveryType" : 1,
"DistributionData" : [
{
"Key" : "Topic",
"Value" : "Topics",
"Children" : null
},
{
"Key" : "Message",
"Value" : "test",
"Children" : null
}
],
"Schedules" : [
ISODate("2016-05-06T05:09:56.988Z")
]
}
]
}
“查询”条件全部使用 $elemMatch 用于文档选择。这实际上是 positional $ operator 所必需的为了获得用于“第一场比赛”的“位置索引”。虽然这实际上不是 positional filtered $[<identifier>] 的“要求”或 positional all $[] 运算符,它仍然有用,因此您甚至不考虑更新文档,因为这些文档不符合 $pull 的后续更新条件。或 arrayFilters选项。
至于 $pull 本身,此处的条件实际上适用于“每个”数组元素,因此不需要 $elemMatch 在该操作中,因为我们已经在查看“元素”级别。
第三个例子表明 positional all $[] 运算符(operator)可以简单地使用那些 $pull 考虑到每个“内部”数组元素的条件,并且只适用于所有“外部”数组元素。所以实际点positional filtered $[<identifier>] expression 是“只”处理那些实际匹配“内部”条件的“外部”数组元素。因此我们为什么使用 $elemMatch 在考虑匹配每个“内部”数组元素时。
如果您实际上至少没有 MongoDB 3.6,那么您正在使用第一种形式,并且可能会重复这种方式,直到更新最终不再返回任何修改过的文档,这表明没有更多的元素与条件匹配。
在 How to Update Multiple Array Elements in mongodb 上有关于“备选方案”的更详细的文章。 ,但只要您的数据适合初始情况,或者您确实有可用的 MongoDB 3.6,那么这就是这里的正确方法。
如果您想查看 MongoDB 3.6 新语法的完整效果。这是我用来在此处验证更新语句的问题中对文档的更改:
{
"_id" : UUID("cf397865-c000-4f51-8959-1aae84769706"),
"CreationDateTime" : ISODate("2016-05-06T05:09:14.589Z"),
"WKT" : "",
"Distributions" : [
{
"_id" : UUID("bb95bedb-4baa-4ada-90b1-0d763e70ebfe"),
"DeliveryType" : 1,
"DistributionData" : [
{
"Key" : "Topic",
"Value" : "Topics",
"Children" : null
},
{
"Key" : null,
"Value" : null,
"Children" : null
},
{
"Key" : "Message",
"Value" : "test",
"Children" : null
},
{
"Key" : null,
"Value" : null,
"Children" : null
}
],
"Schedules" : [
ISODate("2016-05-06T05:09:56.988Z")
]
},
{
"_id" : UUID("bb95bedb-4baa-4ada-90b1-0d763e70ebfe"),
"DeliveryType" : 1,
"DistributionData" : [
{
"Key" : "Topic",
"Value" : "Topics",
"Children" : null
},
{
"Key" : null,
"Value" : null,
"Children" : null
},
{
"Key" : "Message",
"Value" : "test",
"Children" : null
},
{
"Key" : null,
"Value" : null,
"Children" : null
}
],
"Schedules" : [
ISODate("2016-05-06T05:09:56.988Z")
]
}
]
}
这基本上复制了“外部”和“内部”的一些条目,以显示该语句如何删除所有 null值(value)观。
NOTE
arrayFiltersare specified in the "options" argument for.update()and like methods, the syntax is generally compatible with all recent release driver versions and even those prior to the release of MongoDB 3.6.However this is not true of the
mongoshell, since the way the method is implemented there ( "ironically for backward compatibility" ) thearrayFiltersargument is not recognized and removed by an internal method that parses the options in order to deliver "backward compatibility" with prior MongoDB server versions and a "legacy".update()API call syntax.So if you want to use the command in the
mongoshell or other "shell based" products ( notably Robo 3T ) you need a latest version from either the development branch or production release as of 3.6 or greater.Robo 3T notably here is still tied to being based on a MongoDB 3.4 shell. So even when connecting to a capable MongoDB 3.6 instance, these options will not be passed to the server from this program. It is advised to stay with the shell and supported products only, though there are some other offerings which do not have the same limitation.
关于mongodb - 通过多个条件从嵌套数组中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50197062/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似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上找到一
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
这道题是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[