Skip to content

Commit

Permalink
db_mongodb: use $set operator to update a specific list of fields in …
Browse files Browse the repository at this point in the history
…a document

- fix to keep all the other fields unchanged

(cherry picked from commit 2a04558)
  • Loading branch information
mikomarrache authored and miconda committed Feb 12, 2015
1 parent 9b0ed6d commit 1ad7c7d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions modules/db_mongodb/mongodb_dbase.c
Expand Up @@ -1155,7 +1155,7 @@ int db_mongodb_update(const db1_con_t* _h, const db_key_t* _k,
mongoc_collection_t *collection = NULL;
bson_error_t error;
bson_t *mdoc = NULL;
bson_t *udoc = NULL;
bson_t *udoc = NULL, *sdoc = NULL;
char *cname;
char b1;

Expand Down Expand Up @@ -1189,16 +1189,25 @@ int db_mongodb_update(const db1_con_t* _h, const db_key_t* _k,
LM_ERR("cannot initialize update bson document\n");
goto error;
}
sdoc = bson_new();
if(sdoc==NULL) {
LM_ERR("cannot initialize update bson document\n");
goto error;
}
mdoc = bson_new();
if(mdoc==NULL) {
LM_ERR("cannot initialize match bson document\n");
goto error;
}

for(i = 0; i < _un; i++) {
if(db_mongodb_bson_add(udoc, _uk[i], _uv+i, i)<0)
if(db_mongodb_bson_add(sdoc, _uk[i], _uv+i, i)<0)
goto error;
}
if(bson_append_document(udoc, "$set", 4, sdoc)<0) {
LM_ERR("failed to append document to bson document\n");
goto error;
}

if(_o==NULL) {
for(i = 0; i < _n; i++) {
Expand All @@ -1224,12 +1233,14 @@ int db_mongodb_update(const db1_con_t* _h, const db_key_t* _k,
}
bson_destroy (mdoc);
bson_destroy (udoc);
bson_destroy (sdoc);
mongoc_collection_destroy (collection);

return 0;
error:
if(mdoc) bson_destroy (mdoc);
if(udoc) bson_destroy (udoc);
if(sdoc) bson_destroy (sdoc);
if(collection) mongoc_collection_destroy (collection);
return -1;
}
Expand Down

0 comments on commit 1ad7c7d

Please sign in to comment.