How to find the id of document inserted using mongoc_collection_insert_one in mongo-c-driver

35 views Asked by At

I am using the mongo-c-driver, I want to get the id of the document inserted via mongoc_collection_insert_one.

  if (!mongoc_collection_insert_one (collection, insert, NULL, &reply, &error)) {
      fprintf (stderr, "%s\n", error.message);                               
   }                                                      
                                                                      
   str = bson_as_json (&reply, NULL);               
   printf ("%s\n", str);                  

   /* Trying to find the _id from the inserted document */                                                                                  
   bson_iter_init_find (&iter, insert, "_id");                               
   oid = bson_iter_oid (&iter);                           
   bson_oid_to_string (oid, oidstr);   //Crashed             
   printf ("ID: %s\n", oidstr);

Could you please tell us how to find the id of inserted document or if any flag/option that need to be passed in mongoc_collection_insert_one for retrieval of the id ?

0

There are 0 answers