问题描述
我们有一个使用 mongodb 的 Spring Boot 应用程序 (2.2.2)。因为我们想在 AWS 中部署它,所以我们需要迁移到 DocumentDB。但是当我们迁移时,在 MongoDB 中工作的东西不再工作。看下面的代码:
public int getCount(String collection,List<Bson> pipeline,MongoTemplate mongoTemplate) {
List<Bson> newpipeline = new ArrayList<Bson>();
newpipeline.addAll(pipeline);
String cntStr = "{$group:{_id:null,val:{'$sum':1}}}";
newpipeline.add(BasicDBObject.parse(cntStr));
MongoDatabase mongo = mongoTemplate.getDb();
int count = 0;
try {
count = mongo.getCollection(collection).aggregate(newpipeline).first().getInteger("val");
} catch (NullPointerException e) {
} catch (Exception e) {
e.printstacktrace();
}
return count;
}
这在 mongodb 中有效,但在 documentdb 中无效。给出如下错误:
{"timestamp":"2021-03-29T08:29:58.101+0000","status":500,"error":"Internal Server Error","message":"Command Failed with error 18537: 'Could not convert date to string: date component was outside the supported range of 0-9999: 10000' on server xxxx-manager-document-db.cn30hpxqos6b.eu-central-1.docdb.amazonaws.com:27017. The full response is {\"ok\": 0.0,\"operationTime\": {\"$timestamp\": {\"t\": 1617006598,\"i\": 1}},\"code\": 18537,\"errmsg\": \"Could not convert date to string: date component was outside the supported range of 0-9999: 10000\"}","path":"/pmt/api/v1/pm/analytics/getKpiByDimension"}
是否有可能某些游标 api 在 documentDB 中不起作用?
解决方法
您拥有的另一个选择是在 EC2 实例上安装 MongoDB,然后您可以在 Spring BOOT 应用程序中使用 MongoDB API。要了解如何在 EC2 上安装 MongoDB,请参阅:
Install and configure MongoDB community edition
这里有一个创建 Spring BOOT 应用程序的示例,该应用程序使用 MongoDB 存储应用程序数据。