Good Afternoon Everyone,
I have a question regarding upserting arrays to Mongo.
Let's use the following data as an example:
[{
name: 'Charlie Brown',
companies: ['xyz', 'abc'],
data: {
City: 'PHOENIX'
}
},
{
name: 'Alex Brown',
companies: ['xyz', 'ghj'],
data: {
State: 'GA'
}
},
{
name: 'Charlie Brown',
companies: ['abc', 'yui'],
data: {
City: 'FLINT',
State: 'GA'
}
}]
The actual records are a bit more complex and more numerous than 3, but I hope this will serve sufficiently.
I want to insert/update/upsert these records into a mongoDB where matches (updates) do not overwrite the properties, but instead use $addToSet.
The end result should look like this:
[{
name: 'Charlie Brown',
companies: ['xyz', 'abc', 'yui'],
data: {
City: ['PHOENIX', 'FLINT'],
STATE: 'GA'
}
},
{
name: 'Alex Brown',
companies: ['xyz', 'ghj'],
data: {
State: 'GA'
}
}]
I'm having a bit of trouble figuring out the correct mongo syntax for this. I know there is an upsert option on update, but how do I supply an array of records I would like to update, and then update individual records in the array?
I thought maybe the idea is to gather a set of names from the records, so
['Charlie Brown', 'Alex Brown']
and then use that set as a query field,
mongo.collection.update({
['Charlie Brown', 'Alex Brown'],
{$addToSet:{companies: ??? what do I put here??}},
{upsert: true}
})
What is the correct syntax (or correct method, perhaps using aggregations) to insert/update these records into Mongo?
Thank you!
Note: this question has similarities to Bulk upsert in MongoDB using mongoose , but the key here is also using the modifier $addToSet. Also, those solutions place the DB operation within a loop, is this normal with mongo?