Reputation: 31
I save comments for my webpage into MongoDB. Until now it's worked perfectly. As a new comment is saved it appends it to the bottom of the collection. Which means when its loading comments the newest is at the top of the feed which is perfect. Until I added a comment with 124 characters which was added to the end, but now ALL comments posted after are added in the 2nd last position. It now looks as if the 124 character comment is sticked at at the top. All comments under 124 characters don't cause any issues. The only fix is to delete the offending comment. The dates are all correct when posted.
My schema is outlined below, I don't believe that it's the issue.
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 }
});
Here is a reduced version of how I POST the comment information to the DB.
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);
});
});
Here are the last 3 entries into the collection.
/* 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 tried googling in what order MongoDB saves documents in to no success. Any input is appreciated as I am completely lost as to what is causing this.
Upvotes: 1
Views: 44
Reputation: 31
I still don't know why it was storing the documents in the wrong order but I found a way to find and sort my query which solved the problem in the end. Here is my code that I used to do that:
Comment.find({}).sort({timePosted:1}).exec( (err, comments) => {
if (err) { throw err; }
res.json(comments);
});
Upvotes: 0
Reputation: 13765
There is no implicit ordering if you do a find()
query without sorting. MongoDB will just return the documents as it come across them, in an indeterminate order.
It just happens that for a time, you see the documents returned accidentally sorted during your test, until you insert a document that is different from all the others.
If you need the output to be sorted, then you should use a sort()
parameter in your query. Otherwise documents will be returned in no guaranteed order.
Regarding sorting, you should create an index that supports the sort as mentioned in Use Indexes to Sort Query Results. Otherwise, in-memory sort in MongoDB is limited to 32 MB of memory usage. If your result set is >32 MB, the query will exit with an exception.
Upvotes: 2