How to update every document in collection based off another collection with Mongoose

只谈情不闲聊 提交于 2021-02-11 13:43:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!