Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

mongodb - MongoTemplate.getCollectionNames and MongoTemplate,collectionExists method throws StringIndexOutOfBoundsException when connected to Azure Cosmos DB

I am trying to get the list of collections on a database. My application uses spring-boot 2.4.1 and Azure Cosmos DB. Below is the code block.

String uri= "mongodb://<username>:<password>@<hostname.documents.azure.com>:<port>/?ssl=true";
String dbName= "databaseName";
MongoClient mongoClient = MongoClients.create(uri);
SimpleMongoClientDatabaseFactory mongoDbFactory  = 
                 new SimpleMongoClientDatabaseFactory(mongoClient, dbName);
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
System.out.println(mongoTemplate.getCollectionNames());

The code throws a very strange StringIndexOutOfBoundsException.

Stacktrace:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
    at java.lang.String.substring(String.java:1931)
    at com.mongodb.internal.operation.ListCollectionsOperation.projectFromFullNamespaceToCollectionName(ListCollectionsOperation.java:512)
    at com.mongodb.internal.operation.ListCollectionsOperation.access$1500(ListCollectionsOperation.java:86)
    at com.mongodb.internal.operation.ListCollectionsOperation$ProjectingBatchCursor.next(ListCollectionsOperation.java:418)
    at com.mongodb.client.internal.MongoBatchCursorAdapter.next(MongoBatchCursorAdapter.java:64)
    at com.mongodb.client.internal.MongoMappingCursor.next(MongoMappingCursor.java:48)
    at org.springframework.data.mongodb.core.MongoTemplate.lambda$getCollectionNames$28(MongoTemplate.java:2314)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:535)
    at org.springframework.data.mongodb.core.MongoTemplate.getCollectionNames(MongoTemplate.java:2312)
    at TestApp.main(TestApp.java:20)

When I am connecting to a mongo instance that is running on my local with the connection string "mongodb://localhost:27017" the above code works fine.

And my relevant part of the pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.1</version>
  </parent>
  <groupId>org.example</groupId>
  <artifactId>test-mongo</artifactId>
  <version>1.0-SNAPSHOT</version>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>
</dependencies>
</project>

Any thoughts about this error?

question from:https://stackoverflow.com/questions/65836257/mongotemplate-getcollectionnames-and-mongotemplate-collectionexists-method-throw

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you sure your connection uri correctness?

i cant see password part your uri

Add the password section uri like this

String uri= "mongodb://<username>:<password>@...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...