The end goal is to add a new partner to the partners list per pathway object IF they don't already exist. AND if the pathway_name does not equal Engagement Team.
I would want to add the following partner for example:
{
"_id" : ObjectId("5dd5762be13e15a73f042ce5"),
"partner" : ObjectId("5dd57633bf0cbbbb36bc112f")
}
"pathways" : [
{
"_id" : ObjectId("5d837689b57cc711487f1cf0"),
"pathway_name" : "Electronics Technology with Advanced Manufacturing Mechatronics",
"partners" : [
{
"_id" : ObjectId("5d837ab8b57cc711487f1d28"),
"partner" : ObjectId("5d837426b57cc711487f1c96")
}
]
},
{
"_id" : ObjectId("5d837694b57cc711487f1cf1"),
"pathway_name" : "Pre-Mechanical Engineering",
"partners" : [
{
"_id" : ObjectId("5d837acfb57cc711487f1d29"),
"partner" : ObjectId("5d837421b57cc711487f1c92")
}
]
},
{
"_id" : ObjectId("5da6055da0eb361cc8da26dc"),
"pathway_name" : "Engagement Team",
"partners" : [
{
"_id" : ObjectId("5da60711a0eb361cc8da26f0"),
"partner" : ObjectId("5d83749fb57cc711487f1cc2")
}
]
}
],
My current query is as follows, i'm not sure how to add the logic of only adding the partner if they don't exist based off the partner field itself and not the _id field of a partner object.
db.getCollection("schools").updateMany({}, $addToSet:{"pathways$[].partners"})
Expected Result:
"pathways" : [
{
"_id" : ObjectId("5d837689b57cc711487f1cf0"),
"pathway_name" : "Electronics Technology with Advanced Manufacturing Mechatronics",
"partners" : [
{
"_id" : ObjectId("5d837ab8b57cc711487f1d28"),
"partner" : ObjectId("5d837426b57cc711487f1c96")
},
{
"_id" : ObjectId("5dd5762be13e15a73f042ce5"),
"partner" : ObjectId("5dd57633bf0cbbbb36bc112f")
}
]
},
{
"_id" : ObjectId("5d837694b57cc711487f1cf1"),
"pathway_name" : "Pre-Mechanical Engineering",
"partners" : [
{
"_id" : ObjectId("5d837acfb57cc711487f1d29"),
"partner" : ObjectId("5d837421b57cc711487f1c92")
},
{
"_id" : ObjectId("5dd5762be13e15a73f042ce5"),
"partner" : ObjectId("5dd57633bf0cbbbb36bc112f")
}
]
},
{
"_id" : ObjectId("5da6055da0eb361cc8da26dc"),
"pathway_name" : "Engagement Team",
"partners" : [
{
"_id" : ObjectId("5da60711a0eb361cc8da26f0"),
"partner" : ObjectId("5d83749fb57cc711487f1cc2")
}
]
}
],