vibe-d/vibe.d

MongoDB latest version not working

Ecsodikas opened this issue · 14 comments

Hello everyone,

I'm running vibe.d on version 0.9.5 and MongoDB via a docker container on version 6.0.3 (latest tag) with this code:

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;
}

after executing this code I am greeted with the following error:

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/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

for completeness the docker-compose file I use is the following:

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/

in an earlier issue #2693 I got the information that vibe.d does not support mongodb after a specific version, and I don't know if this is the case again. I got it to work by downgrading to version 4.2 (i have not checked another version, it was the first downgrade I tried and it worked).

Edit: version 5.0.12 also works fine.

So I don't know if this problem is already documented or if I do something wrong here, either way I hope you find this informaton somewhat useful.

Have a nice day!

eXodiquas

The MongoDB driver has been revamped by @WebFreak001 for the latest alpha release (mainly in #2691). If you upgrade to 0.9.6-alpha.1, everything should hopefully work.

Hello
This indeed solves the problem of throwing exception, but it always returns null now.

can you post sample code what you are trying? Should work with the alpha in theory.

In MongoDB:

> mongosh
> use webAppMaster
switched to db webAppMaster
> db.webAppMaster.users.findOne({"_id": "testuser", "password" : "1234"})
{ _id: 'testuser', password: '1234' }

in D:

    mClient = connectMongoDB("127.0.0.1");

    auto db     = mClient.getDatabase("webAppMaster");
    writeln(db);
    auto users  = db["users"];
    writeln(users);

    auto result = users.find(["_id" : "testuser"]);
    writeln(result);

This returns an empty array, like [] .

However, I can see

MongoDatabase("webAppMaster", vibe.db.mongo.client.MongoClient)
MongoCollection(vibe.db.mongo.client.MongoClient, MongoDatabase("webAppMaster", vibe.db.mongo.client.MongoClient), "users", "webAppMaster.users")

From the other two writeln calls, which makes me think that it is finding the database and the collection.

that code should emit a deprecation.

The MongoDB calls that emit deprecations may or may not work with MongoDB 5.1+ anymore, just because the protocol internals have changed.

Use the newer methods that the deprecations suggest you to to. (in this case find should tell you to use findOne iirc)

It is not emitting anything. If I use findone, it is printing "null" as the output of the writeln call.

This was the exact code:

auto result = users.findOne(["_id" : "testuser", "password" : "1234"]);

But, compiling with dub run gives me this :

/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/stream/vibe/stream/wrapper.d(301,23): Deprecation: reference to local variable `this` assigned to non-scope parameter `bytes` calling `writeToStream`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/stream/vibe/stream/wrapper.d(301,23): Deprecation: reference to local variable `this` assigned to non-scope parameter `bytes` calling `writeToStream`
Compiling Diet HTML template startPage.dt...
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/stream/vibe/stream/wrapper.d(342,12): Deprecation: reference to local variable `chars` assigned to non-scope parameter `elems` calling `put`
Compiling Diet HTML template fail.dt...
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(911,57): Deprecation: reference to local variable `ret` assigned to non-scope parameter `dst` calling `recv`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(911,57): Deprecation: reference to local variable `ret` assigned to non-scope parameter `dst` calling `recv`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(911,57): Deprecation: reference to local variable `ret` assigned to non-scope parameter `dst` calling `recv`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(751,7): Deprecation: scope variable `data` assigned to non-scope parameter `this` calling `opAssign`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(757,24): Deprecation: scope variable `data` assigned to non-scope parameter `document` calling ``
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(770,8): Deprecation: scope variable `data` assigned to non-scope parameter `this` calling `opAssign`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(777,31): Deprecation: scope variable `data` assigned to non-scope parameter `document` calling ``
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(602,8): Deprecation: scope variable `root` assigned to non-scope parameter `this` calling `opIndex`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(604,8): Deprecation: scope variable `root` assigned to non-scope parameter `this` calling `opIndex`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(606,18): Deprecation: scope variable `root` assigned to non-scope parameter `this` calling `opIndex`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(901,34): Deprecation: scope variable `value` assigned to non-scope parameter `data` calling `sendBytes`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(911,57): Deprecation: reference to local variable `ret` assigned to non-scope parameter `dst` calling `recv`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(517,10): Deprecation: scope variable `root` assigned to non-scope parameter `this` calling `opIndex`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(519,10): Deprecation: scope variable `root` assigned to non-scope parameter `this` calling `opIndex`
/root/.dub/packages/vibe-d-0.9.6-alpha.1/vibe-d/mongodb/vibe/db/mongo/connection.d(521,20): Deprecation: scope variable `root` assigned to non-scope parameter `this` calling `opIndex`

here is my dub.json :

                                                                                                            
{
        "authors": [
                "Sean"
        ],
        "copyright": "Copyright © 2023, Sean",
        "dependencies": {
                "vibe-d": "0.9.6-alpha.1"
        },
        "description": "A simple vibe.d server application.",
        "license": "proprietary",
        "name": "testsite"
}

If i use the example MongoDB code, I get this:

     Linking testsite
     Running testsite 
 [main(----) INF] Connecting to DB...
 [main(----) INF] Querying DB...
 [main(----) INF] Iterating results...
 Warning (thread: main): leaking eventcore driver because there are still active handles
       FD 6 (streamSocket)
 Use '-debug=EventCoreLeakTrace' to show where the instantiation happened
 Warning (thread: main): leaking eventcore driver because there are still active handles
      FD 6 (streamSocket)

Calling ./myapp -debug=EventCoreLeakTrace has no effect (same output). dub run -debug=EventCoreLeakTrace fails (Error: unrecognized switch '-debug=ebug=EventCoreLeakTrace')

That log line means compile with debug version EventCoreLeakTrace (in dub the argument is --debug=EventCoreLeakTrace), but you don't really need that, it's unrelated to the issue you have here.

In the example have you adjusted the query to actually look for something your DB? it looks for {"name": "hans"} by default.

Yes, I have .

test> db.test.test.find()
[
  {
    _id: ObjectId("63e62efad428ac529b0f3444"),
    name: 'hans',
    addr: 'somevalue'
  },
  {
    _id: ObjectId("63e62f0ad428ac529b0f3445"),
    name: 'hans',
    addr: 'somevalue'
  }
]
test> 

mongod --version?

root@instance-1:/home/sean_con# mongod --version
db version v6.0.4
Build Info: {
    "version": "6.0.4",
    "gitVersion": "44ff59461c1353638a71e710f385a566bcd2f547",
    "openSSLVersion": "OpenSSL 1.1.1n  15 Mar 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian11",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Everything, including vibe.d app is running as root.

MongoDB 6.0 and 7.0 have been fixed and are tested as part of CI with the 0.9.8/0.10.0 releases.