Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,14 @@ public Document merge(Document document) {
// if the value is a document, merge it recursively
if (containsKey(key)) {
// if the current document already contains the key,
// then merge the embedded document
get(key, Document.class).merge((Document) value);
// and the value is not null, merge it
Document pairs = get(key, Document.class);
if (pairs != null) {
pairs.merge((Document) value);
} else {
//otherwise, just set the value to whatever was provided
put(key, value);
}
} else {
// if the current document does not contain the key,
// then put the embedded document as it is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

package org.dizitart.no2.integration.collection;

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.DocumentCursor;
import org.dizitart.no2.collection.NitriteCollection;
import org.dizitart.no2.collection.UpdateOptions;
import org.dizitart.no2.collection.*;
import org.dizitart.no2.common.WriteResult;
import org.dizitart.no2.exceptions.NitriteIOException;
import org.dizitart.no2.exceptions.NotIdentifiableException;
Expand Down Expand Up @@ -299,4 +296,18 @@ public void testIssue151() {

assertEquals(coll.find(where("fruit").eq("Apple")).size(), 1);
}

@Test
public void testUpdatePreviouslyNullObject() {
Document doc1 = createDocument().put("fruitType", null);
NitriteCollection coll = db.getCollection("test");

WriteResult insert = coll.insert(doc1);
NitriteId next = insert.iterator().next();
Document doc2 = createDocument().put("_id", next.getIdValue()).put("fruitType", createDocument().put("family", "citric"));
coll.update(doc2);

assertEquals(coll.find(where("_id").eq(next.getIdValue())).size(), 1);
assertNotNull(coll.find(where("_id").eq(next.getIdValue())).firstOrNull().get("fruitType"));
}
}