Search code examples
mongodbamazon-web-servicesspring-bootspring-data-mongodbaws-documentdb

mongodb compatibility with documentdb


We have a spring boot application (2.2.2) which uses mongodb. As we want to deploy it in AWS we need to migrate to DocumentDB. But when we migrate, things which were working in MongoDB are no longer working. See the following code:

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;
}

This works in mongodb, but not in documentdb.Gives error like:

{"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"}

Is it possible that some of the cursor apis do not work in documentDB?


Solution

  • Another option that you have is to install MongoDB on an EC2 instance then you can use the MongoDB API within a Spring BOOT Application. To learn how to install MongoDB on EC2, see:

    Install and configure MongoDB community edition

    There is an example of creating a Spring BOOT app that uses MongoDB to store application data here.

    Creating the MongoDB web application item tracker