Thursday, 3 January 2019

How to add a new field to each record in a collection (MongoDB)

Hi, suppose you had an app with 1000 users signed into it. You then decide that it would be nice to add a followers option in your app (people can follow each other). You would then display the number of followers a user has on his profile dashboard. The default number of followers would be 0.I guess the first step would be to change the user model (using mongoose) and add a followers field to it. However, from what i know even if you add a new field to the model, the existing records will not automatically update.Suppose after the user logs in he will go straight to his profile. He will probably see something like this:'Followers: undefined' instead of 'Followers: 0'What is the way to solve it? I guess it is kind of popular because probably a lot of apps add new fields to their schemas while being online.Thanks in advance ;)

Submitted January 03, 2019 at 08:25AM by everek123

No comments:

Post a Comment