diff --git a/src/index.cpp b/src/index.cpp index ae0ee69e..770168d9 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -296,7 +296,7 @@ Option Index::validate_index_in_memory(nlohmann::json& document, uint3 bool missing_default_sort_field = (!default_sorting_field.empty() && document.count(default_sorting_field) == 0); - if(op != UPDATE && missing_default_sort_field) { + if((op != UPDATE && op != EMPLACE) && missing_default_sort_field) { return Option<>(400, "Field `" + default_sorting_field + "` has been declared as a default sorting field, " "but is not found in the document."); } @@ -309,7 +309,7 @@ Option Index::validate_index_in_memory(nlohmann::json& document, uint3 continue; } - if((a_field.optional || op == UPDATE) && document.count(field_name) == 0) { + if((a_field.optional || op == UPDATE || op == EMPLACE) && document.count(field_name) == 0) { continue; } @@ -320,7 +320,7 @@ Option Index::validate_index_in_memory(nlohmann::json& document, uint3 if(a_field.optional && document[field_name].is_null()) { // we will ignore `null` on an option field - if(op != UPDATE) { + if(op != UPDATE && op != EMPLACE) { // for updates, the erasure is done later since we need to keep the key for overwrite document.erase(field_name); } diff --git a/test/collection_test.cpp b/test/collection_test.cpp index 97993643..2a470070 100644 --- a/test/collection_test.cpp +++ b/test/collection_test.cpp @@ -1250,7 +1250,7 @@ TEST_F(CollectionTest, ImportDocumentsEmplace) { Collection* coll1; std::vector fields = { field("title", field_types::STRING, false, false), - field("points", field_types::INT32, false, true) + field("points", field_types::INT32, false, false) }; coll1 = collectionManager.get_collection("coll1").get(); @@ -1328,7 +1328,7 @@ TEST_F(CollectionTest, ImportDocumentsEmplace) { ASSERT_EQ(1, import_results[1].size()); ASSERT_EQ(1, import_results[1].size()); - // can update individual document via "emplace" + // can update individual document via "emplace" with only partial field (missing points) std::string doc_3_update = R"({"id": "3", "title": "The Superman"})"; auto add_op = coll1->add(doc_3_update, EMPLACE); ASSERT_TRUE(add_op.ok());