折腾:
期间,去试试用mongofiles去删除文件。
delete <filename>
Delete the specified file from GridFS storage.
-》如果两个文件名一样,会有啥现象?
mongodb gridfs delete file
mongofiles delete file
“delete <filename>
Delete the specified file from GridFS storage.
delete_id “<ObjectId>”
New in version 3.2.0.
Delete the specified file from GridFS storage. Specify the file using its _id.”
去试试
➜ 英语资源 mongofiles -d gridfs delete "Otto the Cat.MP3" 2018-03-29T10:16:14.924+0800 connected to: localhost successfully deleted all instances of 'Otto the Cat.MP3' from GridFS
效果:
把名字是:
“Otto the Cat.MP3”
的删除掉了:
> db.fs.files.find().pretty() { "_id" : ObjectId("5abb397ca4bc71fc7d71c7bd"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-28T06:43:08.613Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "/Users/crifan/dev/dev_root/company/xxx/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3" } { "_id" : ObjectId("5abb5fcaa4bc711531cee35c"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-28T09:26:35.121Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "/Users/crifan/dev/dev_root/company/xxx/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3" } { "_id" : ObjectId("5abb6194a4bc71165f9f49f5"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-28T09:34:12.537Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "Otto the Cat" } { "_id" : ObjectId("5abc43a4a4bc712159a35cd9"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-29T01:38:44.853Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "Otto the Cat-withMIME.MP3", "contentType" : "audio/mpeg" }
然后再去,找2个文件名一样的去删除试试:
➜ 英语资源 mongofiles -d gridfs delete "/Users/crifan/dev/dev_root/company/xxx/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3" 2018-03-29T10:17:40.879+0800 connected to: localhost successfully deleted all instances of '/Users/crifan/dev/dev_root/company/xxx/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3' from GridFS
结果:
> db.fs.files.find().pretty() { "_id" : ObjectId("5abb6194a4bc71165f9f49f5"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-28T09:34:12.537Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "Otto the Cat" } { "_id" : ObjectId("5abc43a4a4bc712159a35cd9"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-29T01:38:44.853Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "Otto the Cat-withMIME.MP3", "contentType" : "audio/mpeg" }
再通过id去删除试试,结果报错:
➜ 英语资源 mongofiles -d gridfs delete_id ObjectId("5abb6194a4bc71165f9f49f5") zsh: unknown file attribute: 5
mongofiles delete_id unknown file attribute: 5
后来试了,加上引号即可:
➜ 英语资源 mongofiles -d gridfs delete_id 'ObjectId("5abb6194a4bc71165f9f49f5")' 2018-03-29T10:31:02.611+0800 connected to: localhost successfully deleted file with _id ObjectId("5abb6194a4bc71165f9f49f5") from GridFS
效果,果然删掉了:
> db.fs.files.find().pretty() { "_id" : ObjectId("5abc43a4a4bc712159a35cd9"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-29T01:38:44.853Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "filename" : "Otto the Cat-withMIME.MP3", "contentType" : "audio/mpeg" }
转载请注明:在路上 » 【已解决】用mongofiles去删除GridFS中的文件