GridFS: Clean out all unreferenced files

北慕城南 提交于 2019-12-12 19:24:21

问题


I have just moved towards storing things in my GridFS in MongoDB. During testing, I noticed many files are being created but not deleted properly. I have a collection users, which has a field avatar. It contains the ObjectId of the file.

Now I'd like to have some command I could use to remove all the files and chunks that are not referenced there. Is it possible to do that with one query? Maybe 'map-reduce'?

Also I am not sure how to properly delete GridFS-Files in node-mongodb-native properly.


回答1:


? Now I'd like to have some command I could use to remove all the files and chunks that are not referenced there.

Key terms here is "referenced". MongoDB does not have any joins and therefore, it does not have concept of "references".

Maybe 'map-reduce'?

Map / Reduce is a query tool, not a data modification tool. The same is true of the newer "Aggregration Framework".

What you will have to do is loop through your files and check the references for each one individually. You will then be able to delete those files.

Take a look at some documented examples on how to issue those deletions.



来源:https://stackoverflow.com/questions/17684561/gridfs-clean-out-all-unreferenced-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!