1.帖子有内容有回复,如果在mongodb里只用1个表的话,可以设计成这样
{
title:'巴拉巴拉',
content:'巴拉巴拉',
comments:[
{
user:123,//用户123的回复
content:'123'
},
{
user:456,//用户456的回复
content:'456'
}
]
}
这样的设计的话,如何修改 user:456 里的content?
1)是否用类似数组下标的方式?如:comments[1].content?如何写?
If you already know the subscript of
{user: 456}
, you can change it using array subscript:Or according to the query conditions:
where
$
represents the matched array element. But this way will only modify the first matching array element. So be careful that your conditions must exactly match the element you want to change. Taking your data as an example, if this user has commented twice, there will be a problem with writing like this.