-1

Am running 4.10 Mongo DB version

How to get the count of files filtered like

i have .doc and .pdf and .csv in file system in MOngo.

how to check the count of each format files (.csv ,.pdf,.doc)

Please let me know

joe
  • 73
  • 1
  • 8
  • you need to give us more details. what does your document look like ? what fields store the file names that need to be filtered etc.. – Aditya Jul 29 '19 at 03:49
  • as a starting point, check out this question - https://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like?rq=1 and basically apply a .count() operator at the end to get the result – Aditya Jul 29 '19 at 03:50

1 Answers1

1

I am assuming you don't mean references, but files that are in GridFS. In this case, the way it works is that Mongo simply puts them in 2 collections, one for files meta data, and another collection for chunks of files, that is to say the data of your files that is sliced in pieces. They are regular collections that you can query like any other collection. Their names are "fs.files" and "fs.chunks".

The one you want is "fs.files". For retrieving the files by type, you can use the field contentType. Here is how you get the number of PDFs:

db.fs.files.find({contentType: "application/pdf"}).count()
// or if your question is only for counting
db.fs.files.count({contentType: "application/pdf"})

Like I said, just like any other collection.

EDIT:

var pdfCount = db.fs.files.find({contentType: "application/pdf"}).count();
var csvCount = db.fs.files.find({contentType: "text/csv"}).count();
var docCount = db.fs.files.find({contentType: "application/msword"}).count();
Mig
  • 662
  • 4
  • 13
  • for PDF its working but for csv and doc not working. db.fs.files.find({contentType: "application/pdf"}).count() – joe Jul 29 '19 at 04:55
  • Each of them have a different content type, this is just an example. For `.csv` the contentType is generally `text/csv` and I think `.doc` content type is `application/msword`. If you have trouble finding content type for a file extension, you can check [this list](https://github.com/rack/rack/blob/master/lib/rack/mime.rb#L52-L682) or simply look in your `fs.files` database and check what the content type field says for a specific file. – Mig Jul 29 '19 at 05:11
  • I have edited my answer to include the 3 versions at the end. – Mig Jul 29 '19 at 05:17