Here goes my solution for String, you must adapt for your case.
For the sake of simplicity, my vector is limited to 3, your case 50, just change the code!
require("./connection");
var mongoose = require("mongoose");
const PostSchema = new mongoose.Schema({
User: String,
Posts: [String] //since I am not familar with the notation { type: Array }, I have decided to work with something I am familiar with
});
PostSchema.virtual("posts").set(function(newPost) {
if (this.Posts.length >= 3) {//change here the size to 50
this.Posts.pop();
this.Posts.unshift(newPost);
} else {
this.Posts.unshift(newPost);
}
});
Post = mongoose.model("Post", PostSchema);
Post.findOne({ User: "Jorge Pires" }).then(post => {
post.posts = "this is nice!";
post.save();
console.log(post);
});
//--------------------------------------------------------------
//uncomment for creating your first dataset sample
// Post.create({
// User: "Jorge Pires",
// Posts: ["Hey there", "I am on stack overflow", "this is nice"]
// });
//-----------------------------------------------------------
How it works?
The new elements will enter in the back, and the oldest will be removed if the vector size exceed its limit, mine 3 and yours 50.
It creates the "bubble effect", as you introduce new elements, the oldest ones automatically will move to the head and be eliminated eventually.
references