问题
I have a collection with a single document that contains many stats of different people. It is structured like so:
// Stats list:
[{
id: .... ,
lastUpdated: ... ,
stats: {
Person1: {stat1: 0, stat2: 0, stat3: 0},
Person2: {stat1: 0, stat2: 0, stat3: 0},
...
Person100: {stat1: 0, stat2: 0, stat3: 0}
}
}]
These stats are updated every 24 hours.
Now I have a few hundred listings that contain a specific list of the people in the first collection.
// Listings:
[{
id: ...,
persons: {
Person1: {stat1: 0, stat2: 0, stat3: 0},
Person43: {stat1: 0, stat2: 0, stat3: 0}
}
}]
I want to be able to update all person stats in these listings compared to the persons in the stats section in the first collection I showed.
My logic is something along the lines of this:
await Listing.updateMany({}, { $set: { persons: { "some kind of query" } } });
But I am not sure how to most efficiently go about this.
来源:https://stackoverflow.com/questions/62822273/how-to-update-every-document-in-collection-based-off-another-collection-with-mon