Skip to content

Commit 0c1901d

Browse files
author
Christian Hergert
committed
write-commands: generate proper array indexes for local bson documents.
We were generating empty keys here, which is fine, but not really expected by the lower components. Let's go ahead and just generate the key values.
1 parent 96003b6 commit 0c1901d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/mongoc/mongoc-write-command-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ typedef struct
7676
bson_t writeConcernErrors;
7777
bool failed;
7878
bson_error_t error;
79+
uint32_t upsert_append_count;
7980
} mongoc_write_result_t;
8081

8182

src/mongoc/mongoc-write-command.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,14 +851,22 @@ _mongoc_write_result_append_upsert (mongoc_write_result_t *result,
851851
const bson_value_t *value)
852852
{
853853
bson_t child;
854+
const char *keyptr = NULL;
855+
char key[12];
856+
int len;
854857

855858
BSON_ASSERT (result);
856859
BSON_ASSERT (value);
857860

858-
bson_append_document_begin (&result->upserted, "", -1, &child);
861+
len = bson_uint32_to_string (result->upsert_append_count, &keyptr, key,
862+
sizeof key);
863+
864+
bson_append_document_begin (&result->upserted, keyptr, len, &child);
859865
BSON_APPEND_INT32 (&child, "index", idx);
860866
BSON_APPEND_VALUE (&child, "_id", value);
861867
bson_append_document_end (&result->upserted, &child);
868+
869+
result->upsert_append_count++;
862870
}
863871

864872

@@ -992,7 +1000,11 @@ _mongoc_write_result_merge_arrays (mongoc_write_result_t *result, /* IN */
9921000
bson_iter_t citer;
9931001
int32_t idx;
9941002
int32_t count = 0;
1003+
int32_t aridx;
9951004
bson_t child;
1005+
const char *keyptr = NULL;
1006+
char key[12];
1007+
int len;
9961008

9971009
ENTRY;
9981010

@@ -1001,11 +1013,14 @@ _mongoc_write_result_merge_arrays (mongoc_write_result_t *result, /* IN */
10011013
BSON_ASSERT (iter);
10021014
BSON_ASSERT (BSON_ITER_HOLDS_ARRAY (iter));
10031015

1016+
aridx = bson_count_keys (dest);
1017+
10041018
if (bson_iter_recurse (iter, &ar)) {
10051019
while (bson_iter_next (&ar)) {
10061020
if (BSON_ITER_HOLDS_DOCUMENT (&ar) &&
10071021
bson_iter_recurse (&ar, &citer)) {
1008-
bson_append_document_begin (dest, "", 0, &child);
1022+
len = bson_uint32_to_string (aridx++, &keyptr, key, sizeof key);
1023+
bson_append_document_begin (dest, keyptr, len, &child);
10091024
while (bson_iter_next (&citer)) {
10101025
if (BSON_ITER_IS_KEY (&citer, "index")) {
10111026
idx = bson_iter_int32 (&citer) + result->offset;

0 commit comments

Comments
 (0)