How to convert this custom update query to Java code

Hello, I was finding on the internet how to update all the document field values with lowercase.
I luckily found a query which I modified as per my requirement and it is working correctly.

db.messages.updateMany(, [ $set:  
recipientEmail:  
       $toLower: '$recipientEmail'
 },
senderEmail:  
     $toLower: '$senderEmail'
 }
],  multi: true );

But now I am trying to convert this query into Java code, I am not able to convert it.
I again started looking into the internet, but couldn’t find any code.
So, can anyone help me convert this query to Java code so that I can use it in my Spring Boot application?
Thanks in advance.

Hi @Shrey_Soni and welcome to community forum!!

Please use the below code snipped to use the $set operator:

Arrays.asList(new Document("$set", 
    new Document("recipientEmail", 
    new Document("$toLower", "$recipientEmail"))
            .append("senderEmail", 
    new Document("$toLower", "$senderEmail"))))

The following documentation on Update Many using Java might also be helpful in this case.

Please note that MongoDB Compass and Atlas provides with the feature to export the aggregation pipelines to language specific codes.
Please refer to the documentation for Export Pipeline to Specific Language in compass and Atlas for trying out.

Let us know if you have any further questions.

Regards
Aasawari

2 Likes

Hi @Aasawari , can u tell me how I use the list of documents in mongoTemplate. I don’t see any method accepting List. Also the query that I have share I found it from internet, it is aggregation update query, right now I am not using that version. so is there any way to write java code with version 2.7.1 to set field value to lower case.

Thanks in advance.

Hi @Shrey_Soni

I’m not sure I understand the goal you’re trying to achieve. I think you are trying to convert some part of your document to lower case, and save the result back in the database. Is this correct? Could you post some document example and the result you’re trying to get?

Could you also help us in understanding if Java is a requirement for the application. If not, $merge would help.
Else, please help with the driver and MongoDB versions for the application.
Please refer to the below snippet for further understanding.

 db.test.find()
[
  {
    _id: 0,
    recipientEmail: 'RECIPIENT@EMAIL.COM',
    senderEmail: 'SENDER@EMAIL.COM'
  }
]

 db.test.aggregate([
    { $addFields: { recipientEmail: { $toLower: '$recipientEmail'}, senderEmail:{ $toLower: '$senderEmail'}}},
    { $merge: { into: 'test', on: '_id', whenMatched: 'replace', whenNotMatched: 'insert' } }
])
 db.test.find()
[
  {
    _id: 0,
    recipientEmail: 'recipient@email.com',
    senderEmail: 'sender@email.com'
  }
]

Also, please note that, $merge is available after MongoDB version 4.2 or newer.

Let us know if you have further questions

Best Regards
Aasawari

3 Likes

Hi @Aasawari , the example that you added is correct, we have documents where recipient/sender emails value are in upper case and we won’t to update them in lower case.

I will check how I can use merge from java side.

Thanks for sharing the code.