【发布时间】:2015-07-31 04:49:16
【问题描述】:
我收集了一些论坛帖子,看起来(大致)像
{
"author": author,
"date": new Date(),
"content": req.body.content,
"comments":{
"deleted": 0,
"author": author,
"date": new Date(),
"content": req.body.content
}
}
如果用户添加评论,它只是 $pushed 到当前的 cmets。
现在我想要删除一条特定评论的可能性。 我插入了“已删除”标志以管理员身份查看 cmets,而不是完全删除它们。
但问题是:我如何告诉 mongoDB 我要删除哪个确切的评论?
我刚刚添加了一个“commentID”字段,但我不知道如何自动增加它(文档对此真的很奇怪), 在查询中尝试了很多诸如“cmets.toDeleteId”或“cmets[toDeleteId]”之类的东西之后。
TLDR:我如何查询帖子“34563456354”,评论“5”,$set“deleted”为“1”,其中 cmets 本身就是一个数组?
谢谢
编辑
这是一个帖子的console.dir:
{_id: //etc etc
op: 'Anonymous',
postContent: 'testtest',
createdAt: //etc,
comments:
[ { deleted: 0,
posted: //etc,
author: 'Anonymous',
comment: 'test' },
{ deleted: 0,
posted: //etc,
author: 'Anonymous',
comment: 'testtesttest' } ]
【问题讨论】:
-
是
comments数组还是对象? -
我刚刚检查过,它确实是一个数组,包含对象。我将更新问题中的 console.dir
-
你应该使用另一个评论集合,然后用它填充论坛帖子。
-
有没有办法只插入一个自增字段?那将解决我的问题。比如“把它推到 cmets 上,增加 ID,谢谢”?
-
@PaulSchneider 这里有些混乱你想设置什么
deleted:1comment: 'testtesttest'?或者您想在comments数组中推送新对象?或将deleted增加到 1 其中comment: 'testtesttest'?