5

I am unable to get GridFS working on my computer. I am using .Net nuget MongoDb.Driver and I am trying to upload file to mongodb like this:

MongoClient client = new MongoDB.Driver.MongoClient();
IMongoDatabase dtb = client.GetDatabase("test");
GridFSBucket bucket = new GridFSBucket(dtb);
using (var stream = File.Open(@"<path>", FileMode.Open))
{
    ObjectId id = bucket.UploadFromStream("data", stream);
    var data = bucket.DownloadAsBytes(id);}

But if fails with exception: "MongoDB.Driver.MongoCommandException: Command listIndexes failed: ns does not exist: test.ts.files"

The same code works on other computer, fails on my computer. I even reinstalled the mongo with total cleanup. Weirdly the same function worked at first, then only for one specific database and after few tests nothing works.

I am sorry that I cannot provide any more information.

2
  • Have you figured out a way to fix this problem?
    – Raj
    Commented May 20, 2019 at 16:12
  • No, unfortunatelly
    – Divisadero
    Commented Jun 25, 2019 at 11:46

1 Answer 1

2
+100

Most mongo operations will automatically create collections as needed, but some operations don't. The error message ns does not exist: test.ts.files implies that the namespace (database + collection) doesn't yet exist & needs to be created beforehand (presumably, that's why it's working on one computer but not another -- I bet if you looked at the test database on the working computer, you'd see that the ts.files collection exists, and I believe that different versions of mongo automatically create collections at slightly different times).

The grid fs spec says that when drivers first attempt to upload a file, they should first ensure that the appropriate indexes exist (that's the point at which this is erroring). Creating the necessary fs.files and fs.chunks collections manually should get this code working, but I'm a little surprised that the driver doesn't handle this for you.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.