@@ -97,6 +97,10 @@ _mongoc_write_command_insert_append (mongoc_write_command_t *command,
97
97
}
98
98
}
99
99
100
+ if (command -> u .insert .n_documents ) {
101
+ command -> u .insert .n_merged ++ ;
102
+ }
103
+
100
104
command -> u .insert .n_documents += n_documents ;
101
105
102
106
EXIT ;
@@ -119,6 +123,7 @@ _mongoc_write_command_init_insert
119
123
command -> type = MONGOC_WRITE_COMMAND_INSERT ;
120
124
command -> u .insert .documents = bson_new ();
121
125
command -> u .insert .n_documents = 0 ;
126
+ command -> u .insert .n_merged = 0 ;
122
127
command -> u .insert .ordered = ordered ;
123
128
command -> u .insert .allow_bulk_op_insert = allow_bulk_op_insert ;
124
129
@@ -913,29 +918,19 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
913
918
BSON_ITER_HOLDS_OID (& iter )) {
914
919
result -> nUpserted += 1 ;
915
920
value = bson_iter_value (& iter );
916
- _mongoc_write_result_append_upsert (result , result -> offset , value );
921
+ _mongoc_write_result_append_upsert (result , result -> n_commands , value );
917
922
} else if (bson_iter_init_find (& iter , reply , "upserted" ) &&
918
923
BSON_ITER_HOLDS_ARRAY (& iter )) {
919
924
result -> nUpserted += n ;
920
925
if (bson_iter_recurse (& iter , & ar )) {
921
926
while (bson_iter_next (& ar )) {
922
927
if (BSON_ITER_HOLDS_DOCUMENT (& ar ) &&
923
- bson_iter_recurse (& ar , & citer )) {
924
- idx = 0 ;
925
- value = NULL ;
926
- while (bson_iter_next (& citer )) {
927
- if (BSON_ITER_IS_KEY (& citer , "index" ) &&
928
- BSON_ITER_HOLDS_INT32 (& citer )) {
929
- idx = bson_iter_int32 (& citer );
930
- } else if (BSON_ITER_IS_KEY (& citer , "_id" )) {
931
- value = bson_iter_value (& citer );
932
- }
933
- }
934
- if (value ) {
935
- _mongoc_write_result_append_upsert (result ,
936
- result -> offset + idx ,
937
- value );
938
- }
928
+ bson_iter_recurse (& ar , & citer ) &&
929
+ bson_iter_find (& citer , "_id" )) {
930
+ value = bson_iter_value (& citer );
931
+ _mongoc_write_result_append_upsert (result ,
932
+ result -> n_commands ,
933
+ value );
939
934
}
940
935
}
941
936
}
@@ -961,6 +956,12 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
961
956
break ;
962
957
}
963
958
959
+ result -> n_commands ++ ;
960
+
961
+ if (command -> type == MONGOC_WRITE_COMMAND_INSERT ) {
962
+ result -> n_commands += command -> u .insert .n_merged ;
963
+ }
964
+
964
965
EXIT ;
965
966
}
966
967
@@ -1016,6 +1017,7 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
1016
1017
const bson_value_t * value ;
1017
1018
bson_iter_t iter ;
1018
1019
bson_iter_t citer ;
1020
+ bson_iter_t ar ;
1019
1021
int32_t n_upserted = 0 ;
1020
1022
int32_t affected = 0 ;
1021
1023
@@ -1046,12 +1048,22 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
1046
1048
case MONGOC_WRITE_COMMAND_UPDATE :
1047
1049
if (bson_iter_init_find (& iter , reply , "upserted" )) {
1048
1050
if (BSON_ITER_HOLDS_ARRAY (& iter )) {
1049
- n_upserted = _mongoc_write_result_merge_arrays (result ,
1050
- & result -> upserted ,
1051
- & iter );
1051
+ if (bson_iter_recurse (& iter , & ar )) {
1052
+ while (bson_iter_next (& ar )) {
1053
+ if (BSON_ITER_HOLDS_DOCUMENT (& ar ) &&
1054
+ bson_iter_recurse (& ar , & citer ) &&
1055
+ bson_iter_find (& citer , "_id" )) {
1056
+ value = bson_iter_value (& citer );
1057
+ _mongoc_write_result_append_upsert (result ,
1058
+ result -> n_commands ,
1059
+ value );
1060
+ n_upserted ++ ;
1061
+ }
1062
+ }
1063
+ }
1052
1064
} else {
1053
1065
value = bson_iter_value (& iter );
1054
- _mongoc_write_result_append_upsert (result , 0 , value );
1066
+ _mongoc_write_result_append_upsert (result , result -> n_commands , value );
1055
1067
n_upserted = 1 ;
1056
1068
}
1057
1069
result -> nUpserted += n_upserted ;
@@ -1107,6 +1119,12 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
1107
1119
break ;
1108
1120
}
1109
1121
1122
+ result -> n_commands ++ ;
1123
+
1124
+ if (command -> type == MONGOC_WRITE_COMMAND_INSERT ) {
1125
+ result -> n_commands += command -> u .insert .n_merged ;
1126
+ }
1127
+
1110
1128
EXIT ;
1111
1129
}
1112
1130
0 commit comments