MongoDB+;拉威尔+;jenssegers/laravel mongodb+;更新嵌套的子元素
大家好,我是MongoDB的新手,正在寻找答案MongoDB+;拉威尔+;jenssegers/laravel mongodb+;更新嵌套的子元素,mongodb,laravel,laravel-5.5,jenssegers-mongodb,Mongodb,Laravel,Laravel 5.5,Jenssegers Mongodb,大家好,我是MongoDB的新手,正在寻找答案 是否有任何方法可以在不循环嵌套数组的情况下更新该数组 foreach ($post->comments as $key => $comment) { if ($comment['posted_by'] == $authUser['id']) { $data = $post->update([ "comments.$key.description" => $dataArray['description'
foreach ($post->comments as $key => $comment) {
if ($comment['posted_by'] == $authUser['id']) {
$data = $post->update([
"comments.$key.description" => $dataArray['description'],
"comments.$key.updated_at" => $dataArray['updated_at'],
]);
}}
$post = Post::where('_id', $id)->where('comments.*.id', $commentId)->update(array('description' => $desc));
或者我必须为此编写原始MongoDB查询。
我在主注释下还有一级嵌套注释,所以如果我想更新嵌套注释,我必须循环注释数组,而不是嵌套注释数组
if ($subCommentId) {
foreach ($comment as $nestedkey => $nestedComments) {
if ($nestedComments['id'] === $subCommentId && $nestedComments['posted_by'] == $authUser['id']) {
$data = $post->update([
"comments.$key.$nestedkey.description" => $dataArray['description'],
"comments.$key.$nestedkey.updated_at" => $dataArray['updated_at'],
]);
}
}
}
大概是这样的:
$post = Post::where('_id', $id)->where('comments.*.id', $commentId)->where('comments.*.*.id', $subCommentId)->update(array('description' => $desc));
“注释”:[
{
“说明”:“说明部分”,
“频道”:“swachhata citizen android”,
“用户角色”:“公民”,
“id”:“5b4dc367d282f”,
“用户角色id”:ObjectId(“5accd7f8309a203be03b6441”),
“创建时间”:“2018-07-17 15:52:31”,
“更新时间”:“2018-07-17 15:52:31”,
“ip地址”:“127.0.0.1”,
“用户代理”:“PostmanRuntime/6.4.1”,
“已删除”:false,
“通道id”:“5acccfe4309a2038347a5c47”,
“邮寄人”:编号打印(1),
“评论”:[
{
“说明”:“某些说明嵌套”,
“频道”:“swachhata citizen android”,
“用户角色”:“公民”,
“id”:“5B4DCCFC7022DB”,
“用户角色id”:ObjectId(“5accd7f8309a203be03b6441”),
“创建时间”:“2018-07-17 16:45:19”,
“更新时间”:“2018-07-17 16:45:19”,
“ip地址”:“127.0.0.1”,
“用户代理”:“PostmanRuntime/6.4.1”,
“已删除”:false,
“通道id”:“5acccfe4309a2038347a5c47”,
“邮寄人”:号码打印(1)
}
]
}
]
谢谢。:) 要更新嵌套文档,应使用ArrayFilter:
Post::raw()->updateMany(
[],
[ '$set' => ["comments.$[i].comments.$[j].description" => $desc] ],
[ '$arrayFilters' => [
[
[ "i.id" => "5b4dc367d282f" ],
[ "j.id" => "5b4dcfc7022db" ]
]
]
]
)
希望能有所帮助:)hi@Prafful Panwar,你有什么解决方案吗…@rbvishnu没有,我正在通过foreach$data=$post->update([“comments.$key.comments.$nestedkey.description”=>$dataArray['description'],“comments.$key.comments.$nestedkey.updated\u在”=>$dataArray进行更新['updated_at'],];你试过我的答案吗@Prafful Panwar?