Reputation: 13
I have two collections awards teams have won and a list of teams. I want to ETL the whole of awards into the other (teams) as subdocuments with some minor transformations, but I only get one subdocument from the awards into teams.
Here is the code I used. Teams has a unique index on number and looks like the final document minus the accolade subdocument. However only one accolade subdocument is being added in the array. What am I missing?
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,
});
}
});
Sample awards (teams can win multiple awards and the accolades is to be the collection of awards to aggregate on later):
/* 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"
}
here is what teams turns into with only one accolade added:
{
"_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)"
}]
}
I would have expected two accolades to be added into the array. Do I need to carry the award object id into the teams subdocument?
Upvotes: 1
Views: 733
Reputation: 4065
If you just do $set
it will simply replace the array that is currently there with the one you are putting there. So you have to do a $push
:
{ $push: {accolades: {sku: award_team.sku, acc_category: "Award", acc_name: award_team.name } } }
This will insert a new document into the Array.
Full doc: http://docs.mongodb.org/manual/reference/operator/update/push/
Upvotes: 1