MongoDB的:带2个嵌套数组更新文档中的平均 [英] MongoDB: Updating an average in a document with 2 nested arrays
问题描述
我有以下MongoDB的文档:
I have the following MongoDB document:
{
_id: ObjectId(),
company_name: "Name",
registered: 2/21/2015 2:00,
trucks: [
{
truck_id: "TEB7622",
weight: 88.33,
capacity: 273.333,
length: 378.333,
width: 377.383,
average_grade: 2.5,
grades: [
{
grade_number: 4,
timestamp: 2/21/2015 2:00
}
]
},
{
truck_id: "TEB5572",
weight: 854.33,
capacity: 2735.333,
length: 378.333,
width: 37.383,
average_grade: 3.8,
grades: [
{
grade_number: 4,
timestamp: 2/21/2015 2:00
}
]
}
]
}
我希望每个卡车的 average_grade
加入所有的 grade_numbers
更新。我遇到的问题是, grade_numbers
我想加在一个阵列内的阵列。我一直在使用 $放松
来放松两个卡车和档次阵列尝试。
I want to update each truck's average_grade
by adding all the grade_numbers
. The problem I'm having is that the grade_numbers
I am trying to add are in an array within an array. I have tried using $unwind
to unwind both trucks and grades arrays.
这是我已经尝试使用查询:
This is the query I have tried using:
db.col.aggregate([
{$unwind: "$trucks"},
{$unwind: "$trucks.grades"},
{ $project: {
"_id": "$trucks.truck_id",
"trucks.average_grade": { $avg: { $sum: "trucks.grades.grade_number"} }
}
}])
我需要更多的东西添加到我的查询?我要更新所有的 trucks.average_grades
,因为我试图更新文档中的很多人。
Do I have to add something more to my query? I want to update ALL of the trucks.average_grades
, since there a lot of them in the document I am trying to update.
推荐答案
您不能使用汇聚
更新文件,但你绝对可以用它来得到你的数据要使用更新。首先,我注意到有一些 {}
在你的品位缺少
的等级
阵列。您可能要仔细检查你的文档结构作为发布。其次,与您的聚集查询几个问题。
You cannot use aggregation
to update a document, but you can definitely use it to get the data you want to use for an update. First of all, I noticed that there are some {}
missing around your grade
object inside the grades
array. You might want to double check that your document structure is as posted. Secondly, there are a couple of issues with your aggregation query.
- 的
$平均
运营商工作的$组
子句中,而不是一个$项目
。 - 当您使用
$平均
,你并不需要使用$金额
。 - 您要平均
trucks.grades.grade.grade_number
,其实际持有的档次数值。也就是说,你缺少等级
之间的等级
和GRADE_NUMBER
- The
$avg
operator works inside a$group
clause, not a$project
. - When you use
$avg
, you do not need to use$sum
. - You want to average
trucks.grades.grade.grade_number
, which actually holds the numeric value of the grade. That is, you are missinggrade
betweengrades
andgrade_number
.
如果你解决这些问题,你会得到类似以下的查询:
If you resolve those issues, you get a query similar to the following:
db.col.aggregate([
{ "$unwind": "$trucks" },
{ "$unwind": "$trucks.grades" },
{ "$group":
{
"_id": "$trucks.truck_id",
"average_grade": { "$avg": "$trucks.grades.grade_number" }
}
}
]);
有关示例文档,返回:
{ "_id" : "TEB5572", "average_grade" : 4 }
{ "_id" : "TEB7622", "average_grade" : 4 }
现在你可以使用这个信息来更新 average_grade
字段。如果您使用的MongoDB版本2.6或更高时,总
方法将返回一个指针。您可以通过迭代游标并相应地更新文档。
Now you can use this information to update the average_grade
field. If you're using MongoDB version 2.6 or higher, the aggregate
method will return a cursor. You can iterate through that cursor and update the documents accordingly.
在这个例子中,我搜索具有特定 truck_id
的卡车内
阵列,并继续更新文件在 average_grade
通过一个又聚集查询的计算。您可以修改它来满足您的需求。与聚集查询相结合,code看起来是这样的。
In this example, I search for documents that have a particular truck_id
inside their trucks
array and proceed to update the average_grade
with the one computed by the aggregation query. You can modify it to suit your needs. Combined with the aggregation query, the code looks like this.
// Get average grade for each truck and assign results to cursor.
var cur = db.col.aggregate([
{ "$unwind": "$trucks" },
{ "$unwind": "$trucks.grades" },
{ "$group":
{
"_id": "$trucks.truck_id",
"average_grade": { "$avg": "$trucks.grades.grade_number" }
}
}
]);
// Iterate through results and update average grade for each truck.
while (cur.hasNext()) {
var doc = cur.next();
db.col.update({ "trucks.truck_id": doc._id },
{ "$set": { "trucks.$.average_grade": doc.average_grade }},
{ "multi": true});
}
这篇关于MongoDB的:带2个嵌套数组更新文档中的平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!