0

I am migrating from Mongo DB 4.4.0 to Amazon DocumentDB (with mongodb compatibility) 4.0.

The database migration is done but few errors are occurring in the application. The APIs are in java language and below piece of code is executed when API is called -

executeAPI()

private final MongoDAO statisticsDBService; // Assigned from constructor
List<WriteModel<Document>> statsBulkWrites = new ArrayList<>();

MongoCollection<Document> collection = statisticsDBService.getCollection();
collection.bulkWrite(statsBulkWrites);

Sample JSON Data (statsBulkWrites) -

 [{"$set": {"months": "$months"}}, 
    {"$set": {"months": {"$concatArrays": [{"$filter": {"input": "$months", "cond": {"$not": [{"$in": ["$$this.date", [{"$numberLong": "0000000000000"}]]}]}}}, {"$map": {"input": [{"$numberLong": "0000000000000"}], "in": {"$cond": [{"$in": ["$$this", "$months.date"]}, {"date": "$$this", "value": {"$sum": [{"$arrayElemAt": ["$months.value", {"$indexOfArray": ["$months.date", "$$this"]}]}, {"$numberLong": "000000"}]}}, {"date": "$$this", "value": {"$numberLong": "000000"}}]}}}]}}},
    {"$set": {"sum": {"$sum": "$years.value"}}}] 

The executeAPI() methods gives this error -

com.mongodb.MongoBulkWriteException: Bulk write operation error on server a207849-mktp-docdb-cluster2.csahjzki9umq.us-east-1.docdb.amazonaws.com:27017. 
Write errors: [BulkWriteError{index=0, code=14, message='Wrong type for parameter u', details={}}]. 
    at com.mongodb.connection.BulkWriteBatchCombiner.getError(BulkWriteBatchCombiner.java:173)
    at com.mongodb.connection.BulkWriteBatchCombiner.throwOnError(BulkWriteBatchCombiner.java:202)
    at com.mongodb.connection.BulkWriteBatchCombiner.getResult(BulkWriteBatchCombiner.java:143)
    at com.mongodb.operation.BulkWriteBatch.getResult(BulkWriteBatch.java:227)
    at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:282)
    at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:72)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:205)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:196)
    at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:501)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:196)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:71)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:206)
    at com.mongodb.client.internal.MongoCollectionImpl.executeBulkWrite(MongoCollectionImpl.java:471)
    at com.mongodb.client.internal.MongoCollectionImpl.bulkWrite(MongoCollectionImpl.java:451)
    at com.mongodb.client.internal.MongoCollectionImpl.bulkWrite(MongoCollectionImpl.java:446)
    at io.cloudexchange.market.async.StatsProcessor.processIncr(StatsProcessor.java:221)
    at io.cloudexchange.market.async.StatsProcessor.run(StatsProcessor.java:176)
    at java.base/java.lang.Thread.run(Thread.java:834)

After that I changed BulkWrite to UpdateMany -

for(val setQ: statsBulkWrites) {
            statisticsDBService.getCollection().updateMany(document, setQ);}

Then above code gives this error -

com.mongodb.MongoWriteException: Document can't have $ prefix field names: $concatArrays
    at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1055)
    at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:1032)
    at com.mongodb.client.internal.MongoCollectionImpl.updateMany(MongoCollectionImpl.java:663)
    at com.mongodb.client.internal.MongoCollectionImpl.updateMany(MongoCollectionImpl.java:658)
    at io.cloudexchange.market.async.StatsProcessor.processIncr(StatsProcessor.java:218)
    at io.cloudexchange.market.async.StatsProcessor.run(StatsProcessor.java:176)
    at java.base/java.lang.Thread.run(Thread.java:834)

Please provide a solution.

mradima07
  • 3
  • 1
  • 1
  • 5

1 Answers1

0

You're trying to update documents with aggregation pipeline, which was added in MongoDB 4.2. This is not currently supported in Amazon DocumentDB, not even in the newly released 5.0 version. You'll have to find another way to update the documents, probably the way before MongoDB 4.2, which is to get a cursor for the documents matching the filter and iterate using a loop. See this thread.

Mihai A
  • 351
  • 1
  • 4