mongo c driver - MongoDB CDriver how to update array object in index N? -


i have document looks :

{     "_id" : objectid("5768e43"),     "appid" : {         "number" : 0,     },     sessions : [{             id : 111111,             "setoid" : {                 "number" : 0             },             "custid" : {                 "number" : 0             },          }, {             id : 133333,             "setoid" : {                 "number" : 2             },             "custid" : {                 "number" : 2             },         }, {             id : 7777,             "setoid" : {                 "number" : 2             },             "custid" : {                 "number" : 2             },          },     ] } 

i sessions elment id == 133333 ( in [1] ) , able update new values , add new elements :

{     id : 133333,     "setoid" : {         "number" : 3333      },     "custid" : {         "number" : 4444      },     new_attr_1 : 0     new_attr_2 : 2 }, 

the documentation hard understand when comes c driver can please show best way ?

update
tried cdriver version 1.4 ( latest ) , weird thing happening update did failed ( returend true ) there no update in document

 bson_t *query2 = bcon_new ("sessions.id ", bcon_int32 (133333));      bson_t *update;  update = bcon_new ("$set", "{","sessions.$.new_attr_1" ,bcon_int32 (0) ,"}");  if (!mongoc_collection_update (collection,mongoc_update_none, query2, update, null, &error)) {     fprintf (stderr, "%s\n", error.message);     goto fail; } 

so see allot of strange things happening , how can check if update successful ?

to answer question here solution :

db.doc.update({"sessions.id":133333},               {$set: {"sessions.$.setoid.number":3333,                       "sessions.$.custid.number":4444,                       "sessions.$.new_attr_1" : 0,                       "sessions.$.new_attr_2" : 2                      }               }) 

and c drive should :

static void updatesession( mongo_connection *conn ) {   bson cond[1], op[1];    bson_init( cond );     bson_append_int( cond, "sessions.id",133333);   bson_finish( cond );    bson_init( op );     bson_append_start_object( op, "$set" );       bson_append_int( op, "sessions.$.setoid.number",3333);       bson_append_int( op, "sessions.$.custid.number",4444);       bson_append_int( op, "sessions.$.new_attr_1",0);       bson_append_int( op, "sessions.$.new_attr_2",2);     bson_append_finish_object( op );   bson_finish( op );    mongo_update( conn, "db.doc", cond, op, mongo_update_basic );    bson_destroy( cond );   bson_destroy( op ); } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -