0

so I have two different collections for my social media app. One for the users and the other one for the user's posts. Whenever I'm updating the info from one of my user's collection it should also modify it on the post (since my post collection includes data from the user too), but it's only doing it on the posts that I create after that and not on the ones that I've been creating before. How can I fix it?

USER SCHEMA

const userSchema = new Schema({
    name: { type: String, required: true },
    lastname: { type: String, required: true },
    username: { type: String, required: true },
    email: { type: String, required: true },
    password: { type: String, required: true, minlength: 8 },
    avatar: { data: Buffer, contentType: String },
});

POST SCHEMA

const postSchema = new Schema({
   user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
   name: { type: String, required: true },
   lastname: { type: String },
   username: { type: String },
   avatar: { data: Buffer, contentType: String },
   date: { type: Date, default: Date.now() },
   textOfThePost: { type: String, required: true },
});

EDIT FUNCTION EXPRESS/MONGOOSE

router.put("/edit_profile", async (req, res) => {
  try {
    const { name, lastname, username } = req.body;
    const user = await User.findById(req.user).select("-password");

     if (!user) return res.status(404).json("User doesn't exists");

     if (name) user.name = name;
     if (lastname) user.lastname = lastname;
     if (username) user.username = username;

     await user.save();
     res.json(user);
  } catch (err) {
     res.status(500).json({ error: err.message });
 }
 };
2
  • You would have to update all the old documents manually, it doesn't happen automatically. However, there's no need to actually do any of this; including any user data into a Post document besides the user's ID is completely redundant anyway. All you need to do is populate the user field after getting posts from the DB.
    – user5734311
    Commented Nov 9, 2020 at 15:54
  • But the post model populates the user _id (as it should); how is it possible that only newly created posts populate the user with new data, and the older posts with older user data? They all populate from the same document in the same collection Commented Nov 9, 2020 at 16:01

1 Answer 1

0

You can use updateMany() for that purpose.

const res = await Post.updateMany({ user: user.id  }, { name: user.name, username: user.username /* ... */  });

However as already pointed out you are storing user data redundant on the post model as well as on the user model which is not necessary. Similar to joins in SQL you can simply use populate() and not store any user-related data on your post model. This way everytime you query your posts it will automatically pull the latest matching user model by its id.

myPost.populate('user')

Note that therefore the ref is required, which tells mongoose how to populate the user field.

Not the answer you're looking for? Browse other questions tagged or ask your own question.