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
Related
I have created an Express, Apollo GraphQL server which accepts requests and returns data from a MongoDB database via Prisma.
I have tested my application with a simple database from MongoDB Atlas and it all seems to work fine.
Now that I know everything works fine, I want to 'plug in' a MongoDB database which contains a lot of data already, located in my Azure portal (Cosmos DB).
I have changed the database connection string to point to this new database (a read only connection string) and now attempting to use the Introspection feature of Prisma (https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/mongodb/introspection-typescript-mongodb#introspecting-mongodb-with-prisma).
After running the npx prisma db pull --force command, I get the following error:
I can't figure out if this is an issue with my connection string, or something else.
I have also attempted to use the Read/ Write connection string but that doesn't seem to work either. I get the same error message.
There does not seem to be any information online regarding this error message other than this:
https://www.prisma.io/docs/reference/api-reference/error-reference#prismaclientvalidationerror
But I am unsure if this is at all related, because it seems to relate to creating a new record, rather than anything to do with introspection.
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.
Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.
In the following code, the connection string follows the pattern mongodb://user:password#mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.
However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179
Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.
Environment:
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6
I just had the same error and found out that I had to place the database-name in the connection string.
The documentation here says:
If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
And the user I'm using has no rights to the admin-database, so this is why I received the authentication error.
I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.
So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.
I am in the process of migrating my app from Parse to MongoDB and IBM Bluemix, however I can't seem to reach the server when attempting the migration of data from Parse.
I've been following this tutorial tutorial, and I am currently on the step: Migrating data from Parse.com to your MongoDB instance
I am currently getting this error: No reachable servers when clicking 'Begin the migration'.
These are the connection strings in compose:
And I have tried entering various strings. I assumed this would work:
mongodb://username:password#aws-us-east-1-portal.7.dblayer.com:10803/mtcdatabase
But I get the same error. Obviously I'm changing username and password to my own credentials.
Anyone have any suggestions?
The issue ended up being that I was using the latest version of MongoDB, however Parse doesn't seem to support it. Therefore, I had to use MongoDB Classic version 3.0. Everything works great now.
After running:
import 'package:mongo_dart/mongo_dart.dart';
//Create account with given credentials
createAccount(Map<String, String> credentials) async {
Db db = new Db('mongodb://127.0.0.1/exampledb');
await db.open();
await db.authenticate("user", "password");
//TODO: Post credentials into DB
}
I get an error on the Dart server saying:
Unhandled exception:
Uncaught Error: {ok: 0.0, errmsg: auth failed, code: 18}
And a different error comes up up on the mongod server:
2015-09-27T20:04:25.921+0100 I ACCESS [conn1] Failed to authenticate user#exampledb with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentia
ls missing in the user document
The only time when the authentication has succeeded and has allowed me access to the database is when I am using the command prompt.
Example:
$ use exampledb
$ db.auth("user", "password")
How can I make my Dart script gain access to my local mongodb, using the authenticate method?
Fresh update:
Since version 0.2.5 mongo_dart supports SCRAM-SHA-1 authentification mechanism. It used by default in connections to MongoDb 3
Update:
That seems to be rather widespread problem with some drivers, programs e.t.c not supporting yet new authentication mechanism (SCRAM-SHA-1) of mongodb 3.0
By default, mongodb 3.0, do not create credentials in old format (MONGODB-CR) while creating new users.
There is round-about solution, that force mongodb 3.0 and upper version use MONGODB-CR mode while creating users.
Look for example solution at https://jira.mongodb.org/browse/SERVER-17459
As stated in that thread
both new drivers and legacy software work with that solution
Obviously best solution for mongo_dart would be to add implementation of new authentication mode to the driver. Can not give any date, but I think it should be first feature that I can take in consideration as soon as I have some time for mongo_dart.
And obviously it would be great if somebody beat me at that with pull request :)
Original answer
I've reproduced that error in my environment too.
Error appears to be related to changed default authentication mode in version 3.0 of MongoDb. I'll update this answer, when the problem will be resolved
I tried that in my mongodb 4.0.10 and mongo_dart 0.3.6 and the authentication went all right and correct just update your components if you didn't yet and everything will be fine.
PS: I know I am late but hope someone get used from my "Note".