Support Questions
Find answers, ask questions, and share your expertise

Using PutMongo to Update document in Mongo

Using PutMongo to Update document in Mongo

Explorer

Hello Everyone, 

 

need an help 

 

As per design , input file has following elements in JSON

 

{

 Data1 : “abc”

Data2 {

                Data3: “abc”

           }

}

 

Add some additional elements for tracking.  So the JSON(Documents in Mongo becomes)

 

 

{

 Data1 : “abc”,

Data2 {

                Data3: “abc”,

}

Txn_Date: “30Apr20”,

Txn_Filename: “abc.txt”,

Txn_Response: “Pending”,

Txn_Status: “Awaiting”

}

 

 

Now after procesing the input through Nifi. The json object need to be updated in Mongdb. 

 

I need to update the mongo db with below existing fields updates and some new elements.

Txn_Response : “Processed”,

Txn_Status: “201”.

Id: “123-123-123-123” (new element)

Code : “ch03” (new element)

 

But no able to do that using PutMogo in update mode.

 

1. Tried having only the updated values along with _id in the flow file and option to update with perator/literals. But that is giving error. 

 

Input flow file :  {"code":"3001","_id":"5e98b3b489aad445ecce1658","id":"","Tnx_Status":"Processed","Tnx_Response":"401","Tnx_Date":"2020-04-17-01-06-22"}

PutMongo Configuration : asuuming the flow file should have literals to be updates.

 

But this does NOT Work 

 

Update with whole document in flow file and updatemode as wholedocument

 

2020-04-17 01:16:03,018 ~ INFO ~ o.a.nifi.processors.mongodb.PutMongo ~ PutMongo[id=eb50132b-1010-1171-97d7-562b60402e10] Creating MongoClient ~
2020-04-17 01:16:03,179 ~ ERROR ~ o.a.nifi.processors.mongodb.PutMongo ~ PutMongo[id=eb50132b-1010-1171-97d7-562b60402e10] Failed to insert StandardFlowFileRecord[uuid=6b9d0a0c-047f-4c20-9d26-45ce3db01f94,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1587031592519-2, container=default, section=2], offset=200378, length=135],offset=0,name=Import_CharMaster_USER1_1004200229,size=135] into MongoDB due to com.mongodb.MongoWriteException: After applying the update, the (immutable) field '_id' was found to have been altered to _id: "5e98b3b489aad445ecce1658": com.mongodb.MongoWriteException: After applying the update, the (immutable) field '_id' was found to have been altered to _id: "5e98b3b489aad445ecce1658" ~
com.mongodb.MongoWriteException: After applying the update, the (immutable) field '_id' was found to have been altered to _id: "5e98b3b489aad445ecce1658"
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:523)
at com.mongodb.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:344)

 

flow file with only new values & new fields with updatemode as literal

 

2020-04-17 00:59:22,058 ~ INFO ~ o.a.nifi.processors.mongodb.PutMongo ~ PutMongo[id=eb50132b-1010-1171-97d7-562b60402e10] Creating MongoClient ~
2020-04-17 00:59:22,163 ~ ERROR ~ o.a.nifi.processors.mongodb.PutMongo ~ PutMongo[id=eb50132b-1010-1171-97d7-562b60402e10] Failed to insert StandardFlowFileRecord[uuid=53c45e33-680d-4074-9e0e-b343e31e5483,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1587015539824-1, container=default, section=1], offset=224320, length=173],offset=0,name=Import_CharMaster_USER1_1004200229,size=173] into MongoDB due to java.lang.IllegalArgumentException: Invalid BSON field name code: java.lang.IllegalArgumentException: Invalid BSON field name code ~
java.lang.IllegalArgumentException: Invalid BSON field name code
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:494)
at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:127)

 << File: UpdateModewholedocument.txt >>  << File: UpdatemodeLiteral.txt >>

 

Need help to understand

  1. how PutMongo works in Update mode.
  2.  What should be the Update Query Key, Update Query etc.
  3. What should be update mode. ( it tried both the mode with above JSON but did not work)
  4. what should be the content of the flow file (where the whole document with new values & new fields or it should be only new fields & new values)

Please help if screenshot if any. 

 

Thanks , 

 

Rups.