mongodb 与 documentdb 的兼容性

mongodb compatibility with documentdb

我们有一个 spring 引导应用程序 (2.2.2),它使用 mongodb。因为我们想在 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.Gives 错误,例如:

{"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 存储应用程序数据。

Creating the MongoDB web application item tracker