GridFS throws Command listIndexes failed exception when uploading file - mongodb

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.

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.

Related

Mongodump not dumping all records

I am using mongodump(version 2.4.14) to create a db backup that I restore on another system using mongorestore(version 2.4.14) but all the records are not being dumped and restored in the target mongo(2.4.14) instance.
I have tried:
Restoring the db to two separate instances of mongo and the problem persists on both.
Mongoexport with queries - Mongoexport document export count and db.collection.count does not match.
While trying to debug this, I came across this link in which others came across the same problem but no solution for the problem is mentioned.
I am looking for a any help in regards to finding out what the problem might be, how can I debug this further.
Update - Mongoexport/import a particular query response and importing it to a freshly created database works fine. The issue arises only when importing to existing db.

Mongo update to 3.2 gives error 'GridFS: Index with name: files_id_1_n_1 already exists with different options'

After updating mongo from 3.0 to 3.2, I get the following error when trying to put a new file in my gridFS with mongofiles:
2016-10-25T15:23:10.765+0200 Failed: error while storing 'execute.sh' intoGridFS: Index with name: files_id_1_n_1 already exists with different options
As a result the files are partially inserted in the GridFS. I can see the entries in the collections .chunks and .files but I cannot get the stored files with mongofiles or Java driver.
The solution was to drop the .file collection index files_id_1_n_1.
I did it with robomongo but you can do it also directly with mongo console
db.getCollection('configs.chunks').dropIndex('files_id_1_n_1')
Then I just put a new file with mongofiles and it has recreated the index.
I have re-indexed by security but maybe it was not necessary.
db.getCollection('configs.chunks').reIndex()
I did not see any difference between the two indexes and maybe my solution is not the best one but it worked.

MongoDB "j must be numeric or a boolean value"

I've set up my own local mongodb (v. 3.0.2) instance on a local ubuntu version (14.10) and I'm using genghis(v. 2.3.11) to visualize it. My programm is able to create new documents in the database, but when I try to save a newly created document or delete a document in genghis it always returns "j must be numeric or a boolean value" but it still creates/deletes the document. The error doesn't show up when I edit a document. The only thing I could find when I tried to find a solution on google was this: https://github.com/mongodb/mongo/blob/master/src/mongo/db/write_concern_options.cpp which makes me think that it's a problem with my mongodb setup (and has nothing to do with genghis), but I do not know how to resolve this.
Have you tried running the code against Mongo 2?
I ran into this same error when I tried connecting to Mongo 3 from a service that was using client libraries intended for Mongo 2.

MongoDB with a Tomcat v7 Java Server

I've written a simple Java server. The basic idea is that, upon clicking a button, a servlet will load some data into MongoDB.
However, whenever I try to actually load something into MongoDB (in my case, saving a collection), I get the following error:
com.mongodb.MongoException$Network: can't call something : /127.0.0.1:27017/
com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:295)
com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
com.mongodb.DB.command(DB.java:274)
com.mongodb.DB.command(DB.java:256)
com.mongodb.DB.command(DB.java:313)
com.mongodb.DB.command(DB.java:211)
com.mongodb.DB.createCollection(DB.java:170)
servlets.ImportData.doPost(ImportData.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Does anyone know what the issue here might be? When I searched this problem I found people guessing at everything from port conflicts to stale connections to incorrect installation of MongoDB, so it would be fantastic to get some clarity on this one.
If it helps to see the source code, I'd be more than happy to share it.
Thanks in advance!
Turns out my instance of mongod wasn't running at the time I was executing the commands.
Make sure to run "mongod" in your console at all times you're using the server.
The second error I encountered was failing to specify the host and port of the MongoClient, in the following way:
MongoClient mongo = new MongoClient();
Instead you can do:
MongoClient mongo = new MongoClient("localhost", 27017);
You can stick that into your servlet context and pass it around as needed to other servlets, and while I have declared MongoClient mongo with the new statement here, I would recommend keeping it as a class variable of the ContextListener.

Dart with MongoDB

Are there any recent working examples on using Dart with MongoDB. All of the samples I'm trying are getting errors. Example below.
Code:
import 'package:mongo_dart/mongo_dart.dart';
main(){
Db db = new Db("mongo-dart-blog"); // Throws an error.
}
Error:
Unhandled exception:
Invalid scheme in uri: mongo-dart-blog
#0 Db.Db (package:mongo_dart/src/database/db.dart:25:7)
#1 main (file:///.../MongoDart/app.dart:4:11)
I believe you are running some old versions of mongo_dart samples.
I belive if you would get fresh version either from github https://github.com/vadimtsushko/mongo_dart or from pub.dartlang.org samples and tests would run successfully.
Corresponding line in fresh version of blog sample looks like:
Db db = new Db("mongodb://127.0.0.1/mongo_dart-blog");
And this is excerpt from comment for Db.open method
Db constructor expects valid mongodb URI.
For example next code points to local mongodb server on default mongodb port, database testdb
var db = new Db('mongodb://127.0.0.1/testdb');
And that code direct to MongoLab server ds037637-a.mongolab.com on 37637 port, database blog, username dart, password test
var db = new Db('mongodb://dart:test#ds037637-a.mongolab.com:37637/blog');
Unfortunately API DOC on github site is very stale, due to old dartdoc bug:
http://code.google.com/p/dart/issues/detail?id=5218
I hope it will be fixed soon and I'll be able to generate valid API doc for mongo_dart.
I've had a lot of trouble with MongoDB in client-side Dart myself. I ended up moving the Mongo calls to the back end, and using a combination of REST and Json to communicate between the two ends. You can find an example I wrote at https://github.com/RossBabcock3/dartgo3