Vibe.d MongoDB database connection

eXodiquas exodiquas at gmail.com
Sun Dec 25 23:05:08 UTC 2022


Hello everyone,

I tried to fix this problem myself but I just can't get it to 
work and you guys were great help for me on the last problems I 
encountered, so I give it a shot again. :P

I worked through the "D Web Development" book by Kai Nacke and I 
tried to setup a MongoDB connection for my application.

I'm running vibe.d on version 0.9.5.

```d
this.client = 
connectMongoDB("mongodb://root:abc@127.0.0.1:27017");
auto db = this.client.getDatabase("test");
MongoCollection users = db["users"];
foreach(d; users.find(Bson.emptyObject)) {
     d.writeln;
}
```

This is close to the code I found in the [vibe.d 
documentation](https://vibed.org/docs#mongo). But it gives me a 
huge error message like this:

```
MongoDB reply was longer than expected, skipping the rest: 223 
vs. 36
vibe.db.mongo.connection.MongoDriverException at ../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/cursor.d(304): Query failed. Does the database exist?
----------------
/usr/include/dlang/dmd/std/exception.d:518 @safe noreturn 
std.exception.bailOut!(vibe.db.mongo.connection.MongoDriverException).bailOut(immutable(char)[], ulong, scope const(char)[]) [0x560171989396]
/usr/include/dlang/dmd/std/exception.d:439 @safe bool 
std.exception.enforce!(vibe.db.mongo.connection.MongoDriverException).enforce!(bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) [0x56017198930e]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/cursor.d:304 @safe void vibe.db.mongo.cursor.MongoCursorData!(vibe.data.bson.Bson).MongoCursorData.handleReply(long, vibe.db.mongo.flags.ReplyFlags, int, int) [0x5601719822b6]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/connection.d:462 @safe int vibe.db.mongo.connection.MongoConnection.recvReply!(vibe.data.bson.Bson).recvReply(int, scope void delegate(long, vibe.db.mongo.flags.ReplyFlags, int, int) @safe, scope void delegate(ulong, ref vibe.data.bson.Bson) @safe) [0x560171984a84]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/connection.d:322 @safe void vibe.db.mongo.connection.MongoConnection.query!(vibe.data.bson.Bson).query(immutable(char)[], vibe.db.mongo.flags.QueryFlags, int, int, vibe.data.bson.Bson, vibe.data.bson.Bson, scope void delegate(long, vibe.db.mongo.flags.ReplyFlags, int, int) @safe, scope void delegate(ulong, ref vibe.data.bson.Bson) @safe) [0x560171984147]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/cursor.d:367 @safe void vibe.db.mongo.cursor.MongoFindCursor!(vibe.data.bson.Bson, vibe.data.bson.Bson, ).MongoFindCursor.startIterating() [0x560171982ab3]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/cursor.d:233 @property @safe bool vibe.db.mongo.cursor.MongoCursorData!(vibe.data.bson.Bson).MongoCursorData.empty() [0x560171982106]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mongo/cursor.d:60 @property @safe bool vibe.db.mongo.cursor.MongoCursor!(vibe.data.bson.Bson).MongoCursor.empty() [0x560171981e33]
source/database.d:12 database.DatabaseConnection 
database.DatabaseConnection.__ctor() [0x56017199dc7d]
source/app.d:10 _Dmain [0x56017197044b]
Error Program exited with code 1

```

It asks me if the database exists in this error message and the 
answer is yes, because I can access it via mongo-express and I 
can see that the database, the collection and a user is in there.

My MongoDB setup is a docker environment running a mongodb 
container and a mongodb-express container via docker-compose.

```yaml
version: '3.1'

services:
   db:
     image: mongo
     restart: always
     ports:
       - 27017:27017
     environment:
       MONGO_INITDB_ROOT_USERNAME: root
       MONGO_INITDB_ROOT_PASSWORD: abc

   mongo-express:
     image: mongo-express
     restart: always
     ports:
       - 8081:8081
     environment:
       ME_CONFIG_MONGODB_ADMINUSERNAME: root
       ME_CONFIG_MONGODB_ADMINPASSWORD: abc
       ME_CONFIG_MONGODB_URL: mongodb://root:abc@db:27017/
```

I exposed the 27017 port to the host system, which should be 
sufficient for a connection via the given connection url 
`mongodb://root:abc@127.0.0.1:27017`.

The only difference from my setup to the one in the book or in 
the documentation is the docker setup. But I can't figure out why 
this should be a problem.

Does somebody know what's up with this problem?

Thanks everyone in advance and happy holidays!

eXodiquas


More information about the Digitalmars-d-learn mailing list