我找不到一个简单的附加解决方案,所以我最终用一个新文件替换了以前的文件,并删除了旧文件。
$stream = $bucket->openDownloadStream($this->file_id);
$prev_content = stream_get_contents($stream);
//delete old data chunks
DB::connection('mongodb')->collection('fs.chunks')->where('files_id',$this->file_id)->delete();
//delete old file
DB::connection('mongodb')->collection('fs.files')->where('_id',$this->file_id)->delete();
$new_content =$prev_content.$dt.$msg."\n"; // append to log body
$new_filename = $filename_prefix;
$stream = $bucket->openUploadStream($new_filename);
fwrite($stream, $new_content);
fclose($stream);
$new_file = DB::connection('mongodb')->collection('fs.files')->where('filename',$new_filename)->first();
更新: