www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vibe.d MongoDB database connection

reply eXodiquas <exodiquas gmail.com> writes:
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 ../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/
ongo/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).bailOu
(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/d
/mongo/cursor.d:304  safe void
vibe.db.mongo.cursor.MongoCursorData!(vibe.data.bson.Bson).MongoCursorDa
a.handleReply(long, vibe.db.mongo.flags.ReplyFlags, int, int) [0x5601719822b6]
../../../../.dub/packages/vibe-d-0.9.5/vibe-d/mongodb/vibe/db/mo
go/connection.d:462  safe int
vibe.db.mongo.connection.MongoConnection.recvReply!(vibe.data.bson.
son).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/mo
go/connection.d:322  safe void
vibe.db.mongo.connection.MongoConnection.query!(vibe.data.bson.Bson).quer
(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/d
/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/d
/mongo/cursor.d:233  property  safe bool
vibe.db.mongo.cursor.MongoCursorData!(vibe.data.bson.Bson).Mon
oCursorData.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
Dec 25 2022
parent reply eXodiquas <exodiquas gmail.com> writes:
On Sunday, 25 December 2022 at 23:05:08 UTC, eXodiquas wrote:
 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 looked a bit closer into the problem and I found an issue in the vibe.d repository that states that the current version of vibe.d is not compatible with MongoDB version >= 5.1 https://github.com/vibe-d/vibe.d/issues/2693. In the same comment there is also a hint that a fix is already merged into vibe.d. I'll open an issue, because this is probably a vibe.d problem. I got it to work by downgrading my MongoDB container.
Dec 26 2022
parent Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 26 December 2022 at 21:32:51 UTC, eXodiquas wrote:
 I looked a bit closer into the problem and I found an issue in 
 the vibe.d repository that states that the current version of 
 vibe.d is not compatible with MongoDB version >= 5.1
the mongo driver in the standalone package shares some code with vibe's but handled this 5.1 thing a bit differently and *might* work idk i haven't tested on one that new https://code.dlang.org/packages/mongo-standalone
Dec 26 2022