我有两个奖项获奖团队和团队名单。我希望将整个奖项ETL归入其他(团队)作为子文档,并进行一些小的转换,但我只从团队中获得一个子文档。
这是我使用的代码。团队有一个唯一的数字索引,看起来像最终文件减去了accolade子文档。但是,数组中只添加了一个accolade子文档。我错过了什么?
db.awardout.find().forEach(function(award_team){
if(award_team != null)
{
db.teams.findAndModify(
{
query: {number: award_team.team},
update: { $set: {accolades: [ {sku: award_team.sku, acc_category: "Award", acc_name: award_team.name }] } },
upsert: true,
});
}
});
样品奖(团队可以赢得多个奖项,奖项将是以后汇总的奖项集合):
/* 18 */
{
"_id" : ObjectId("552077289ad9c19e809965de"),
"sku" : "RE-VRC-14-0110",
"name" : "Tournament Finalists (VRC/VEXU)",
"team" : "44",
"qualifies" : [],
"order" : "1"
}
/* 35 */
{
"_id" : ObjectId("552077289ad9c19e809965ef"),
"sku" : "RE-VRC-14-0110",
"name" : "Robot Skills Winner (VRC/VEXU)",
"team" : "44",
"qualifies" : [],
"order" : "4"
}
这里是团队变成的,只添加了一个荣誉:
{
"_id" : ObjectId("552071f69ad9c19e80996432"),
"program" : "VRC",
"robot_name" : "Fred VII",
"organisation" : "Green Egg Robotics",
"region" : "Massachusetts",
"country" : "United States",
"number" : "44",
"team_name" : "Green Egg Robotics",
"city" : "Oakham",
"grade" : "High School",
"is_registered" : "0",
"accolades" : [{
"sku" : "RE-VRC-14-0110",
"acc_category" : "Award",
"acc_name" : "Think Award (VRC/VEXU)"
}]
}
我原本期望在阵列中添加两个荣誉。我是否需要将奖励对象ID带入团队子文档?
答案 0 :(得分:1)
如果您只是$set
,它只会将当前存在的阵列替换为您放置的阵列。所以你必须做$push
:
{ $push: {accolades: {sku: award_team.sku, acc_category: "Award", acc_name: award_team.name } } }
这会将新文档插入数组。
完整文档:http://docs.mongodb.org/manual/reference/operator/update/push/