我有收款账户:
{
"_id": ObjectId("571a0e145d29024733793cfe"),
"name": "Cash",
"user": ObjectId("571a0e145d29024733793cfc")
}
和交易。事务可以有不同的字段:
{
"_id": ObjectId("571a0e145d29024733793d06"),
"amount": 100,
"type": "earned",
"account": ObjectId("571a0e145d29024733793cfe"),
"user": ObjectId("571a0e145d29024733793cfc"),
},
{
"_id": ObjectId("571a0e145d29024733793d04"),
"amount": 300,
"type": "spent",
"account": ObjectId("571a0e145d29024733793cfe"),
"user": ObjectId("571a0e145d29024733793cfc")
},
{
"_id": ObjectId("571a0e145d29024733793d07"),
"amount": 100,
"type": "transfer",
"sourceAccount": ObjectId("571a0e145d29024733793cfd"),
"destinationAccount": ObjectId("571a0e145d29024733793cfe"),
"user": ObjectId("571a0e145d29024733793cfc"),
}
我想按每个账号做一个分组统计。 我写了一个聚合框架查询到数据库:
db.transactions.aggregate([
{ $match: { user: user._id } },
{
$group: {
_id: '$account',
earned: {
$sum: {
$cond: [{ $eq: ['$type', 'earned'] }, '$amount', 0]
}
},
spent: {
$sum: {
$cond: [{ $eq: ['$type', 'spent'] }, '$amount', 0]
}
},
deposits: {
$sum: {
$cond: [{ $eq: ['$type', 'transfer'] }, '$amount', 0]
}
},
withdrawal: {
$sum: {
$cond: [{ $eq: ['$type', 'transfer'] }, '$amount', 0]
}
},
maxEarned: {
$max: {
$cond: [{ $eq: ['$type', 'earned'] }, '$amount', 0]
}
},
maxSpent: {
$max: {
$cond: [{ $eq: ['$type', 'spent'] }, '$amount', 0]
}
},
count: { $sum: 1 }
}
}
]);
但它不能正常工作。它仅适用于现场账户的交易。 我想按现场帐户或 sourceAccount 或 destinationAccount 分组。
我还尝试在“_id”字段中写入:
_id: { account: '$account', sourceAccount: '$sourceAccount', destinationAccount: '$destinationAccount' }
或
_id: {$or: ['$account', '$sourceAccount', '$destinationAccount']}
或
_id: {$in: ['$account', '$sourceAccount', '$destinationAccount']}
但它会导致错误的分组或不起作用。
如何对不同领域进行分组?
最佳答案
使用 $cond 运算符作为 _id 表达式,根据您指定的条件按键评估组。 <强> $cond 运算符使用比较运算符 $gt 来评估确定字段是否存在的 bool 表达式并使用此 comparison order 强>。因此,您可以按照以下示例重组您的管道:
db.transactions.aggregate([
{
"$group": {
"_id": {
"$cond": [
{ "$gt": [ "$account", null ] },
"$account",
{
"$cond": [
{ "$gt": [ "$sourceAccount", null ] },
"$sourceAccount",
"$destinationAccount"
]
}
]
},
"count": { "$sum": 1 }
}
}
])
示例输出
{ "_id" : ObjectId("571a0e145d29024733793cfd"), "count" : 1 }
{ "_id" : ObjectId("571a0e145d29024733793cfe"), "count" : 2 }
关于MongoDB聚合组_id按不同字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36795528/
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?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,如果没有检查,请帮助我,非常感谢,谢谢
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
我几天前在我的rubyonrails2.3.2上安装了Sphinx和Thinking-Sphinx,基本搜索效果很好。这意味着,没有任何条件。现在,我想用一些条件过滤搜索。我有公告模型,索引如下所示:define_indexdoindexestitle,:as=>:title,:sortable=>trueindexesdescription,:as=>:description,:sortable=>trueend也许我错了,但我注意到只有当我将:sortable=>true语法添加到这些属性时,我才能将它们用作搜索条件。否则它找不到任何东西。现在,我还在使用acts_as_tag
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案
因此,当我遵循MichaelHartl的RubyonRails教程时,我注意到在用户表中,我们为:email属性添加了一个唯一索引,以提高find的效率方法,因此它不会逐行搜索。到目前为止,我们一直在根据情况使用find_by_email和find_by_id进行搜索。然而,我们从未为:id属性设置索引。:id是否自动索引,因为它在默认情况下是唯一的并且本质上是顺序的?或者情况并非如此,我应该为:id搜索添加索引吗? 最佳答案 大多数数据库(包括sqlite,这是RoR中的默认数据库)会自动索引主键,对于RailsMigration
我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona
我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我