Storing comments for my webpage in MongoDB has been flawless so far. Whenever a new comment is saved, it gets added to the bottom of the collection. This results in the newest comment appearing at the top when loading comments, which is ideal. However, I encountered an issue when adding a 124-character comment - all subsequent comments are now being positioned second to last. It seems like the 124-character comment is stuck at the top. Comments with less than 124 characters do not cause any problems. The only workaround currently is to delete the problematic comment. The timestamps for each comment are correct.
Although I don't think it's the root cause of the problem, below is the schema utilized:
var commentSchema = new Schema({
username: { type: String },
comment: { type: String },
timePosted: { type: Date, dafault: new Date() },
upVotes: { type: Number, default: 0 },
downVotes: { type: Number, default: 0 }
});
Outlined here is a simplified version of how the comment information is posted to the database:
router.post('/addComment', function(req, res, next) {
comment = new Comment(req.body);
comment.comment = comment.comment.replace(/</g, "<").replace(/>/g, ">");
comment.save(function(err, savedComment) {
if (err) { throw err; }
res.json(savedComment);
});
});
The three latest entries in the collection are as follows:
/* 48 */
{
"_id" : ObjectId("5dc33349673dc30ed49d53dc"),
"upVotes" : 0,
"downVotes" : 0,
"comment" : "unce unce unce",
"username" : "mcChicken",
"timePosted" : ISODate("2019-11-06T20:55:37.955Z"),
"__v" : 0
}
/* 49 */
{
"_id" : ObjectId("5dc3334d673dc30ed49d53dd"),
"upVotes" : 0,
"downVotes" : 0,
"comment" : "yes",
"username" : "mcChicken",
"timePosted" : ISODate("2019-11-06T20:55:41.927Z"),
"__v" : 0
}
/* 50 */
{
"_id" : ObjectId("5dc2e1dd7d13ba16effdeaa7"),
"upVotes" : 0,
"downVotes" : 0,
"comment" : "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"username" : "mcChicken",
"timePosted" : ISODate("2019-11-06T15:08:13.237Z"),
"__v" : 0
}
I have attempted researching the order in which MongoDB saves documents but haven't found a solution yet. Any insight would be greatly appreciated as I am perplexed by this issue.