MongoDB client

Hello to all! I want to migrate some services I wrote in Node to Crystal. Is there a library to connect to MongoDB Atlas?

I tried several, but none of them worked. Is it because of the version?

Hi @krthr,

I published a new version of Cryomongo fixing some issues preventing connection to Atlas (I tried with MongoDB 4.2 & 4.4).

Could you check the new release? (just make sure to pick the url having the the mongodb:// scheme, not mongodb+srv://)

Hi! Thank you. It worked without +srv… I’m using Moongoon, but I’m having troubles with reference (populate in Mongoose)

Hi. I run in prod using datanoise/mongo.cr without any ORM stuff. It costs some boilerplate and effort, but nothing terrible. You could also consider one of its forks. Mine is marzhaev/mongo.cr, but there should better maintained ones.

I know quite a bit of time has passed since the last discussion, but I cannot get the current MongoDB Atlas working with the Cryomongo driver, even after removing the +srv. Is there a workaround in place for this currently?

Hey @JSONBash :wave:,

I just tried connecting to a free cluster on Atlas (mongo version 5.0.14) and did not encounter any particular issue with the lastest cryomongo.

I used this piece of code to connect:

client = Mongo::Client.new("mongodb+srv://<login>:<password>@<cluster_address>/?retryWrites=true&w=majority")

database = client["test_db"]
collection = database["test_coll"]

collection.delete_many(BSON.new)

collection.insert_one({one: 1})
puts collection.find.to_pretty_json

Which outputs:

[
  {
    "_id": {
      "$oid": "63e97d8a51dfbd430bb3e5a9"
    },
    "one": 1
  }
]

I guess you did this already, but can you double check that your IP is whitelisted?

It drove me nuts for 45 minutes while trying to reproduce your problem - the server was closing the connection without any noticeable reason. Fortunately I remembered to check the network access settings on Atlas.

Thanks for taking some time to help me out @elbywan :)

I should have provided more information in my original post so I apologize. I also tried with a free shared cluster (5.0.14) and had no issues. But when I try with a serverless version 6.1 database I encounter the issue (I don’t see how to downgrade version on the serverless option). This is the stack trace:

Unhandled exception: Invalid uri: mongodb+srv://<username>:<password>@<serverless_address>/?retryWrites=true&w=majority, Invalid TXT record option: loadBalanced (Mongo::Error)
  from lib/cryomongo/src/cryomongo/uri/uri.cr:106:5 in 'parse'
  from lib/cryomongo/src/cryomongo/client.cr:65:55 in 'initialize:connection_string:options:start_monitoring'
  from lib/cryomongo/src/cryomongo/client.cr:60:5 in 'initialize'
  from lib/cryomongo/src/cryomongo/client.cr:59:3 in 'new'
  from src/contexts/db.cr:43:1 in '__crystal_main'
  from /opt/homebrew/Cellar/crystal/1.7.2/share/crystal/src/crystal/main.cr:115:5 in 'main_user_code'
  from /opt/homebrew/Cellar/crystal/1.7.2/share/crystal/src/crystal/main.cr:101:7 in 'main'
  from /opt/homebrew/Cellar/crystal/1.7.2/share/crystal/src/crystal/main.cr:127:3 in 'main'

Hopefully this sheds a little more light as to what is causing the error.

1 Like

Thanks for taking some time to help me out @elbywan :)

You’re welcome :smiley:

Hopefully this sheds a little more light as to what is causing the error.

So by looking at the logs it seems like the mongodb specs have changed and there is a new loadBalanced field that “must” (from their terminology) be supported by drivers.

Unfortunately I do not have access to a cluster on Atlas with a mongo version >= 6.

I pushed a commit on cryomongo#master to allow this new option, could you check if it solves the connection issue :crossed_fingers:?
(you’ll need to add branch: master in the cryomongo section of the shard.yml file)

@elbywan Unfortunately there is another, different error now.

2023-02-13T17:42:25.956665Z  ERROR - mongo: Monitoring handshake error: Code: 354 - The server is being accessed through a load balancer, but this driver does not have load balancing enabled
2023-02-13T17:42:25.960284Z  ERROR - mongo: Monitoring handshake error: Code: 354 - The server is being accessed through a load balancer, but this driver does not have load balancing enabled

I don’t want to waste your time debugging this, is there a private way I can send you a login to my serverless database for testing?

I don’t want to waste your time debugging this, is there a private way I can send you a login to my serverless database for testing?

Unfortunately after further investigation, it seems like connecting to a mongodb serverless atlas instance requires full driver support for the load balancing spec (which also implies supporting the new hello handshake command).

This is quite a huge change which would impact the logic of multiple parts of the driver, and do not think that I will have the necessary bandwidth in the near future to work on this change myself :confused:.

I’m sorry about this, maybe someone would be willing to contribute :crossed_fingers:.