[sr-dev] git:4.2:1ad7c7d0: db_mongodb: use $set operator to update a specific list of fields in a document

Daniel-Constantin Mierla miconda at gmail.com
Thu Feb 12 09:53:22 CET 2015


Module: kamailio
Branch: 4.2
Commit: 1ad7c7d0a3a534ed1d388391d16fb2c715a24419
URL: https://github.com/kamailio/kamailio/commit/1ad7c7d0a3a534ed1d388391d16fb2c715a24419

Author: mikomarrache <mikomarrache at users.noreply.github.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2015-02-12T09:51:25+01:00

db_mongodb: use $set operator to update a specific list of fields in a document

- fix to keep all the other fields unchanged

(cherry picked from commit 2a045585efc9a8e5b17123d320539d0c8cb6be77)

---

Modified: modules/db_mongodb/mongodb_dbase.c

---

Diff:  https://github.com/kamailio/kamailio/commit/1ad7c7d0a3a534ed1d388391d16fb2c715a24419.diff
Patch: https://github.com/kamailio/kamailio/commit/1ad7c7d0a3a534ed1d388391d16fb2c715a24419.patch

---

diff --git a/modules/db_mongodb/mongodb_dbase.c b/modules/db_mongodb/mongodb_dbase.c
index b48a5c0..808575b 100644
--- a/modules/db_mongodb/mongodb_dbase.c
+++ b/modules/db_mongodb/mongodb_dbase.c
@@ -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;
 
@@ -1189,6 +1189,11 @@ 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");
@@ -1196,9 +1201,13 @@ int db_mongodb_update(const db1_con_t* _h, const db_key_t* _k,
 	}
 
 	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++) {
@@ -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;
 }




More information about the sr-dev mailing list