2

i have document that looks like this :

{
    "_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 like to get the sessions elment which its id == 133333 ( which is in [1] ) and be able to update it with new values and add new elements to it so it will look like this :

{
    id : 133333,
    "setOID" : {
        "Number" : 3333

    },
    "custID" : {
        "Number" : 4444

    },
    new_attr_1 : 0
    new_attr_2 : 2
},

The documentation is very hard to understand when it comes to C Driver can someone please show what it the best way to do it ?

UPDATE
I tried with cdriver version 1.4 ( latest ) and weird thing is happening the update did failed ( returend true ) but there is no update in the 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 as you see allot of strange things happening , how can i check if update is really successful ?

1 Answer 1

1

To answer your question here is a 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 with the C Drive should be something like that :

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 );
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but i need the C driver way man , The C driver is nightmare
what is the "$" dollar sign? i tring to convert it to BCON_NEW
Here you have an example of updating document with C driver api.mongodb.com/c/0.4/tutorial.html
Thanks for the answer , the problem is im using version 1.4 of the dirver Where mongo_update is no more , but i will try to update it to the current one
i updated the question with my try with the new Cdriver
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.