Skip to content

Commit ba688d4

Browse files
committed
Fixing the update command - hopefully
1 parent 36ed6fb commit ba688d4

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/main/java/picoded/dstack/mongodb/MongoDB_DataObjectMap.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,61 @@ public Map<String, Object> DataObjectRemoteDataMap_get(String _oid) {
167167
* either partially (if supported / used), or completely
168168
**/
169169
public void DataObjectRemoteDataMap_update(String _oid, Map<String, Object> fullMap,
170-
Set<String> keys) {
170+
Set<String> updateKeys) {
171171

172172
// Configure this to be an "upsert" query
173173
FindOneAndUpdateOptions opt = new FindOneAndUpdateOptions();
174174
opt.upsert(true);
175175

176-
// Generate the document to "upsert"
177-
Document doc = new Document();
178-
for (String key : keys) {
179-
doc.put(key, fullMap.get(key));
176+
// Generate the document of changes
177+
// See: https://www.mongodb.com/docs/manual/reference/operator/update/setOnInsert/
178+
//
179+
// We do this via the various, set/unset/etc operators
180+
Document set_doc = new Document();
181+
Document setOnInsert_doc = new Document();
182+
Document unset_doc = new Document();
183+
184+
// Lets iterate the keys, and decide accordingly
185+
Set<String> fullKeys = fullMap.keySet();
186+
for( String key : fullKeys ) {
187+
// Get the value
188+
Object value = fullMap.get(key);
189+
190+
// Special _oid handling
191+
if( key.equals("_oid") ) {
192+
setOnInsert_doc.append("_oid", _oid);
193+
continue;
194+
}
195+
196+
// Lets apply the update values
197+
if( updateKeys.contains(key) ) {
198+
// Handle NULL values unset
199+
if( value == null || value == ObjectToken.NULL ) {
200+
unset_doc.append(key, "");
201+
continue;
202+
}
203+
204+
// Handle values update
205+
set_doc.append(key, value);
206+
}
207+
208+
// OK - this is not in the update dataset
209+
// meaning we should do a "setOnInsert" if its not null
210+
if( value == null || value == ObjectToken.NULL ) {
211+
// does nothing
212+
} else {
213+
setOnInsert_doc.append(key, value);
214+
}
180215
}
181-
// Enforce _oid
182-
doc.put("_oid", _oid);
216+
217+
// Generate the "update" doc
218+
Document updateDoc = new Document();
219+
updateDoc.append("$set", set_doc);
220+
updateDoc.append("$setOnInsert", setOnInsert_doc);
221+
updateDoc.append("$unset", unset_doc);
183222

184223
// Upsert the document
185-
collection.findOneAndUpdate(Filters.eq("_oid", _oid), doc, opt);
224+
collection.findOneAndUpdate(Filters.eq("_oid", _oid), updateDoc, opt);
186225
}
187226

188227
//--------------------------------------------------------------------------

src/test/java/picoded/dstack/mongodb/MongoDB_conn_test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ public void connectivityTest() {
156156
assertEquals(null, resMap.get("_id"));
157157
assertEquals("the world is both big and small", resMap.get("messsage"));
158158

159-
160159
//-------------------------------------
161160
// D : Delete the document !
162161
//-------------------------------------

0 commit comments

Comments
 (0)