我正在尝试使用聚合函数将我的数据转换为数组列表。 下面是我的代码
Quantity.aggregate([
{$group: {
_id: {
product_asin: "$productAsin",
parent_asin: "$parentProductAsin",
total_quantity: "$totalQuantity" ,
}
}},
{ "$project":{product_asin:true,parent_asin:true,total_quantity:true,_id:false}}
])
这给了我以下输出
[ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ]
如果我删除 $project,我会得到以下输出,其中添加了 _id 以及数据
[ { _id:
{ product_asin: 'asdasd',
parent_asin: 'Dasda',
total_quantity: '0' } },
{ _id:
{ product_asin: 'dfasf',
parent_asin: 'fasd',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fasd',
parent_asin: 'fasd',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fasd',
parent_asin: 'asd',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fsda',
parent_asin: 'asdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'asd',
parent_asin: 'asd',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fsda',
parent_asin: 'asdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fasd',
parent_asin: 'asdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fsda',
parent_asin: 'asdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fsda',
parent_asin: 'asdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fsadfsa',
parent_asin: 'asdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'asdf',
parent_asin: 'sadf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fsda',
parent_asin: 'asdfasdf',
total_quantity: '0' } },
{ _id:
{ product_asin: 'fasda',
parent_asin: 'fasd',
total_quantity: '0' } } ]
问题: 如何更改聚合函数代码以获得以下输出
{product_asin: 'IBM', parent_asin: 13, total_quantity: 12},
{product_asin: 'IB2342M', parent_asin: 13, total_quantity: 12},
{product_asin: 'I44234BM', parent_asin: 14, total_quantity: 12},
最佳答案
当您将 $group 与多个字段一起使用时,所有这些字段都在 _id 中,因此您在 $group 之后的文档将看起来像您在问题中添加的结果,要解决此问题,您可以将您想要的字段添加到顶级对象。
在您的 $group 之后添加另一个管道以添加这些字段:
{
$addFields: {
total_quantity: "$_id.total_quantity",
parent_asin: "$_id.parent_asin",
product_asin: "$_id.product_asin"
}
}
然后像以前一样执行$project。
关于mongodb - 聚合函数增强以包含多个字段而没有 _id 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56104751/
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子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?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,如果没有检查,请帮助我,非常感谢,谢谢
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我有一个具有一些属性的模型: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
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了