how to use mongo unset on keys starting with something which may or may not existsin java

Viewed 64

I have a requirement where i need to update mongo documents everyday based on its current status. So, i might have an random[1],random[2] fields today but in tomorrow's run i might not need that field in the document based on the account status. To do this i need to remove the fields starting with "random" if any for the document before doing anything and then redo only if its valid. I know about unset and objecttoarray but not sure how i can achieve this.

I am trying to use following code but it doesn't work -

 Document query = new Document();
 query.put("Email","test123@testone.com");
 Document update = new Document();
 update.put("$unset", new Document("$match",new 
 Document("$regex","random.*")));
 db.getDatabase().getCollection("ColName").updateOne(query, update);
0 Answers
Related