@@ -197,21 +197,27 @@ public String setValueRaw(String key, String value, long expire) {
197197
198198 // Generate the document of changes
199199 // See: https://www.mongodb.com/docs/manual/reference/operator/update/setOnInsert/
200- Document set_doc = new Document ();
201- set_doc .append ("val" , value );
200+
201+ // Generate the "update" doc
202+ Document updateDoc = new Document ();
203+ Document set_doc = new Document ();
202204
203- // Expire timestamp if its configured, else it should be ignored
204- if (expire > 0 ) {
205- set_doc .append ("expireAt" , new Date (expire ));
205+ // Expire timestamp if its configured, else it should be removed
206+ if (expireAt > 0 ) {
207+ set_doc .append ("expireAt" , new Date (expireAt ));
208+ } else {
209+ Document unset_doc = new Document ();
210+ unset_doc .append ("expireAt" , "" );
211+ updateDoc .append ("$unset" , unset_doc );
206212 }
207213
214+ // Setup the value on update/insert/upsert
215+ set_doc .append ("val" , value );
216+ updateDoc .append ("$set" , set_doc );
217+
208218 // Set the key on insert
209219 Document setOnInsert_doc = new Document ();
210220 setOnInsert_doc .append ("key" , key );
211-
212- // Generate the "update" doc
213- Document updateDoc = new Document ();
214- updateDoc .append ("$set" , set_doc );
215221 updateDoc .append ("$setOnInsert" , setOnInsert_doc );
216222
217223 // Upsert the document
0 commit comments