Home > Net >  how to replace and update mongoose schema
how to replace and update mongoose schema

Time:08-10

Hi I have a User schema I just want to update and replace it here's the example I have a followUsers schema like

followUser: [
_id:String
]

I'd like to replace it

followUser: [
_id:String;
username:String;
profileImage:String;
]

the problem is that my website already has users and users have following users is there a way to replace the schema and update it? thanks for reading my question.

CodePudding user response:

This method of getting the follower object in the user database is not correct, and you have to make complex queries to discuss updates and make queries. It would be better to have a separate follow database. To solve your problem, the best way to write a script is to find all the users and create a separate function that will create the array you want and save it in that user. You can use Promis.all() for this. for example:

        User.find({}) 
             .then((users)=> {  
                 Promise.all(users.map(user => {
                 
                  return updateFollowerArray(user)
                })) })

   
  • Related