我将网页注释保存到MongoDB中。到现在为止,它一直运行良好。保存新注释后,它将其添加到集合的底部。这意味着当其加载注释为最新时,它位于供稿的顶部,这是完美的。直到我添加了一个带有124个字符的注释,该注释被添加到末尾,但是现在所有发布的注释都添加到了倒数第二个位置。现在,看起来好像124个字符的注释位于顶部。 124个字符以下的所有注释均不会引起任何问题。唯一的解决方法是删除有问题的注释。发布后的日期都是正确的。
下面概述了我的架构,我不认为这是问题所在。
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 }
});
这是我将评论信息发布到数据库的简化版本。
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);
});
});
这是收藏集中的最后3个条目。
/* 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"),"comment" : "yes","timePosted" : ISODate("2019-11-06T20:55:41.927Z"),"__v" : 0
}
/* 50 */
{
"_id" : ObjectId("5dc2e1dd7d13ba16effdeaa7"),"comment" : "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","timePosted" : ISODate("2019-11-06T15:08:13.237Z"),"__v" : 0
}
我尝试用什么顺序查询MongoDB将文档保存到哪个顺序失败。感谢所有输入,因为我完全不知道是什么原因造成的。