最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】用mongofiles去删除GridFS中的文件

file crifan 1334浏览 0评论
折腾:
【已解决】把本地的音频字幕等数据存储到本地MongoDB数据库中
期间,去试试用mongofiles去删除文件。
mongofiles — MongoDB Manual
delete <filename>
Delete the specified file from GridFS storage.
-》如果两个文件名一样,会有啥现象?
mongodb gridfs delete file
mongodb – How to delete an image file from GridFS by file metadata? – Stack Overflow
gridfs – Tools for working with GridFS — PyMongo 3.6.1 documentation
Deleting and Renaming Files
mongofiles delete file
MongoDB GridFS – How to use mongofiles(or some other utility) to delete all files? – Stack Overflow
mongofiles — MongoDB Manual 3.6
“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中的文件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.162 seconds, using 22.16MB memory