I can connect using the mongosh, but have seen various exceptions when trying to connect with a java client directly. The most recent one I can’t get past when using a direct copy paste from the docs is the following…
ServerApi api = ServerApi.builder().version(ServerApiVersion.V1).build();
String uri = "mongodb+srv://retail_user:***************@cluster0.********.mongodb.net/Cluster0?retryWrites=true&w=majority";
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(uri))
.serverApi(api)
.build();
MongoClient mongoClient = MongoClients.create(settings);
The exception thrown is
The method serverApi(ServerApi) is undefined for the type MongoClientSettings.Builder
I pasted the same code into my editor and it compiles fine with 4.7. You should double-check that you’re actually using the right driver version, as that method was added all the way back in the 4.3 release.
Thanks, Jeff. You were correct, I had an old jar in the tomcat lib directory that had an old version that in fact did not have the noted method. I can compile, but am getting an authn error for the same thing that again, works with mongosh. This is a simple as it gets, and I have tried various versions of embedding the user/pwd in the URL, using a MongoCredential object, but all return the same thing.
Thanks again, Jeff, I appreciate the eyes on it. That’s interesting, as I would also expect it to work, but I keep getting an authn error with both the embedded URI and the credential object. I used the starter python example, and that works fine, so something is amuck in java, but I don’t know what. I am a partner of Mongo’s with a tech talk tomorrow and wanted to use MongoDB as a sink in a demo, but the java piece has taken too many cycles. I am sure it is me and something simple I am missing, but I will just use python with Flask for a web part of the demo. This works…
>>> from pymongo import MongoClient
>>> CONNECTION_STRING = "mongodb+srv://retail-user:*************@cluster0.********.mongodb.net/Cluster0"
>>> client = MongoClient(CONNECTION_STRING)
>>> dbname = client['Cluster0']
>>> collection_name = dbname["inventory"]
>>> item_details = collection_name.find_one()
>>> for item in item_details:
... print(item)
...
_id
TS
SKU
QUANTITY
TYPE
>>>