static bool get_upserted_id (const bson_t *update, bson_value_t *upserted_id) { bson_iter_t iter; bson_iter_t id_iter; /* Versions of MongoDB before 2.6 don't return the _id for an upsert if _id * is not an ObjectId, so find it in the update document's query "q" or * update "u". It must be in one or both: if it were in neither the _id * would be server-generated, therefore an ObjectId, therefore returned and * we wouldn't call this function. If _id is in both the update document * *and* the query spec the update document _id takes precedence. */ bson_iter_init (&iter, update); if (bson_iter_find_descendant (&iter, "u._id", &id_iter)) { bson_value_copy (bson_iter_value (&id_iter), upserted_id); return true; } else { bson_iter_init (&iter, update); if (bson_iter_find_descendant (&iter, "q._id", &id_iter)) { bson_value_copy (bson_iter_value (&id_iter), upserted_id); return true; } } /* server bug? */ return false; }
static void test_bson_iter_find_descendant (void) { bson_iter_t iter; bson_iter_t desc; bson_t *b; b = get_bson(BINARY_DIR"/dotkey.bson"); assert(bson_iter_init(&iter, b)); assert(bson_iter_find_descendant(&iter, "a.b.c.0", &desc)); assert(BSON_ITER_HOLDS_INT32(&desc)); assert(bson_iter_int32(&desc) == 1); bson_destroy(b); b = BCON_NEW ("foo", "{", "bar", "[", "{", "baz", BCON_INT32 (1), "}", "]", "}"); assert (bson_iter_init (&iter, b)); assert (bson_iter_find_descendant (&iter, "foo.bar.0.baz", &desc)); assert (BSON_ITER_HOLDS_INT32 (&desc)); assert (bson_iter_int32 (&desc) == 1); bson_destroy (b); b = BCON_NEW ("nModified", BCON_INT32 (1), "n", BCON_INT32 (2)); assert (bson_iter_init (&iter, b)); assert (bson_iter_find_descendant (&iter, "n", &desc)); assert (!strcmp (bson_iter_key (&desc), "n")); bson_destroy (b); b = BCON_NEW ("", BCON_INT32 (1), "n", BCON_INT32 (2)); assert (bson_iter_init (&iter, b)); assert (bson_iter_find_descendant (&iter, "n", &desc)); assert (!strcmp (bson_iter_key (&desc), "n")); bson_destroy (b); }
/************************************************************************************************************************* F_SECUNDARIA: Obteniendo el VALOR dlong int IP, puerto o numBloque para una copiaX de un bloque X TESTEADO =) ****************************/ long int getValorDeUnaCopia_ArchivoDB (const bson_t * documento_X, int numBloque, int numDeCopia, char * claveRequerida) { bson_iter_t punteroDeBusqueda; bson_iter_t valorEncontrado; long int valorRequerido; // GENERO LA CLAVE DE BUSQUEDA //Ejemplo "Bloqlong int.IP" Bloques.numBloque.ArrayDeCopias.numDeCopia.claveRequerida (clavlong int IP o puerto o numDeBloque) char *claveGenerada = string_new(); string_append(&claveGenerada, "Bloques"); string_append(&claveGenerada, "."); string_append(&claveGenerada, string_itoa(numBloque)); string_append(&claveGenerada, ".copias."); string_append(&claveGenerada, string_itoa(numDeCopia-1)); string_append(&claveGenerada, "."); string_append(&claveGenerada, claveRequerida); if (documento_X != NULL && bson_iter_init (&punteroDeBusqueda, documento_X) && bson_iter_find_descendant (&punteroDeBusqueda, claveGenerada, &valorEncontrado) && BSON_ITER_HOLDS_INT32 (&valorEncontrado)) { valorRequerido = bson_iter_int32 (&valorEncontrado); } free(claveGenerada); return valorRequerido; }
//Devuelve un num positivo o Devuelve 0 si hay error int getCantidadDeCopiasDe1Bloque_ArchivoDB(const bson_t * documento_X, int numBloque) { bson_iter_t punteroDeBusqueda; bson_iter_t valorEncontrado; int CantidadDeCopias = 0; char *claveGenerada = string_new(); string_append(&claveGenerada, "Bloques"); string_append(&claveGenerada, "."); string_append(&claveGenerada, string_itoa(numBloque)); string_append(&claveGenerada, "."); string_append(&claveGenerada, "cantidadDeCopias"); if (documento_X != NULL && bson_iter_init (&punteroDeBusqueda, documento_X) && bson_iter_find_descendant (&punteroDeBusqueda, claveGenerada, &valorEncontrado) && BSON_ITER_HOLDS_INT32 (&valorEncontrado)) { CantidadDeCopias = bson_iter_int32 (&valorEncontrado); } free(claveGenerada); return CantidadDeCopias; }
bson_value_t* _aggregate_get_value_at_key(bson_t *doc, char *key) { bson_iter_t iter; bson_iter_t child_iter; if (!bson_iter_init (&iter, doc) || !bson_iter_find_descendant (&iter, key, &child_iter)) { return NULL; } bson_value_t *result = bson_malloc(sizeof(bson_value_t)); bson_value_copy(bson_iter_value(&child_iter), result); return result; }
static bool _mongoc_matcher_op_exists_match (mongoc_matcher_op_exists_t *exists, /* IN */ const bson_t *bson) /* IN */ { bson_iter_t iter; bson_iter_t desc; bool found; BSON_ASSERT (exists); BSON_ASSERT (bson); found = (bson_iter_init (&iter, bson) && bson_iter_find_descendant (&iter, exists->path, &desc)); return (found == exists->exists); }
static bool _mongoc_matcher_op_type_match (mongoc_matcher_op_type_t *type, /* IN */ const bson_t *bson) /* IN */ { bson_iter_t iter; bson_iter_t desc; BSON_ASSERT (type); BSON_ASSERT (bson); if (bson_iter_init (&iter, bson) && bson_iter_find_descendant (&iter, type->path, &desc)) { return (bson_iter_type (&iter) == type->type); } return false; }
//Casts to an int int get_bson_int(const bson_t *obj, char *name) { bson_iter_t i; bson_iter_t t; if (bson_iter_init(&i, obj)) { if (bson_iter_find_descendant(&i, name, &t)) { if (bson_iter_type(&t) == BSON_TYPE_INT32) { return bson_iter_int32(&t); } if (bson_iter_type(&t) == BSON_TYPE_DOUBLE) { return (int) bson_iter_double(&t); } } } return 0; }
/************************************************************************************************************************* F_SECUNDARIA: Obteniendo CANTIDAD TOTAL DE BLOQUES que conforman un Archivo TESTEADO =) ****************************/ int getCantidadTotalDeBloques_ArchivoDB(const bson_t * documento_X) //Devuelve un num positivo o Devuelve 0 si hay error { bson_iter_t punteroDeBusqueda; bson_iter_t valorEncontrado; int CantidadBloques = 0; if (documento_X != NULL && bson_iter_init (&punteroDeBusqueda, documento_X) && bson_iter_find_descendant (&punteroDeBusqueda, "cantidadDeBloques", &valorEncontrado) && BSON_ITER_HOLDS_INT32 (&valorEncontrado)) { CantidadBloques = bson_iter_int32 (&valorEncontrado); } return CantidadBloques; }
static bool _mongoc_matcher_op_compare_match (mongoc_matcher_op_compare_t *compare, /* IN */ const bson_t *bson) /* IN */ { bson_iter_t tmp; bson_iter_t iter; BSON_ASSERT (compare); BSON_ASSERT (bson); if (strchr (compare->path, '.')) { if (!bson_iter_init (&tmp, bson) || !bson_iter_find_descendant (&tmp, compare->path, &iter)) { return false; } } else if (!bson_iter_init_find (&iter, bson, compare->path)) { return false; } switch ((int)compare->base.opcode) { case MONGOC_MATCHER_OPCODE_EQ: return _mongoc_matcher_op_eq_match (compare, &iter); case MONGOC_MATCHER_OPCODE_GT: return _mongoc_matcher_op_gt_match (compare, &iter); case MONGOC_MATCHER_OPCODE_GTE: return _mongoc_matcher_op_gte_match (compare, &iter); case MONGOC_MATCHER_OPCODE_IN: return _mongoc_matcher_op_in_match (compare, &iter); case MONGOC_MATCHER_OPCODE_LT: return _mongoc_matcher_op_lt_match (compare, &iter); case MONGOC_MATCHER_OPCODE_LTE: return _mongoc_matcher_op_lte_match (compare, &iter); case MONGOC_MATCHER_OPCODE_NE: return _mongoc_matcher_op_ne_match (compare, &iter); case MONGOC_MATCHER_OPCODE_NIN: return _mongoc_matcher_op_nin_match (compare, &iter); default: BSON_ASSERT (false); break; } return false; }
/** * Copies over raw BSON data into Monary column storage. This function * determines the types of the data, dispatches to an appropriate handler and * copies over the data. It keeps a count of any unsuccessful loads and sets * NumPy-compatible masks on the data as appropriate. * * @param coldata A pointer to monary_column_data which contains the final * storage location for the BSON data. * @param row The row number to store the data in. Cannot exceed * coldata->num_rows. * @param bson_data A pointer to an immutable BSON data buffer. * * @return The number of unsuccessful loads. */ int monary_bson_to_arrays(monary_column_data* coldata, unsigned int row, const bson_t* bson_data) { bson_iter_t bsonit; bson_iter_t descendant; int i; int masked; int success; monary_column_item* citem; if (!coldata || !bson_data) { DEBUG("%s", "Array pointer or BSON data was NULL and could not be loaded."); return -1; } if (row > coldata->num_rows) { DEBUG("Tried to load row %d, but that exceeds the maximum # of rows (%d) ", row, coldata->num_rows); return -1; } masked = 0; for (i = 0; i < coldata->num_columns; i++) { success = 0; citem = coldata->columns + i; // Use the iterator to find the field we want bson_iter_init(&bsonit, bson_data); if (bson_iter_find_descendant(&bsonit, citem->field, &descendant)) { success = monary_load_item(&descendant, citem, row); } // Record success in mask if (citem->mask != NULL) { citem->mask[row] = !success; } if (!success) { masked++; } } return masked; }
static int mongo_get_user_key(u08bits *usname, u08bits * realm, hmackey_t key) { // mongoc_collection_t * collection = mongo_get_collection("turnusers_lt"); mongoc_collection_t * collection = mongo_get_collection("users"); if(!collection) return -1; bson_t query; bson_init(&query); BSON_APPEND_UTF8(&query, "auth.email", (const char *)usname); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "auth.secret", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &query, &fields, NULL); int ret = -1; if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'turnusers_lt'\n"); } else { { // DEBUG /* const bson_t * doc = 0; char *str = 0; while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); fprintf (stdout, "%s\n", str); bson_free (str); } */ } const bson_t * item; uint32_t length; bson_iter_t iter; const char* password; if (mongoc_cursor_next(cursor, &item)) { bson_iter_t sub_iter; if (bson_iter_init(&iter, item) && bson_iter_find_descendant(&iter, "auth.secret", &sub_iter) && BSON_ITER_HOLDS_UTF8(&sub_iter)) { password = bson_iter_utf8(&sub_iter, &length); size_t sz = get_hmackey_size(SHATYPE_DEFAULT) * 2; char skey[sizeof(hmackey_t) * 2 + 1]; password2hmac(password, usname, realm, skey); if(convert_string_key_to_binary(skey, key, sz / 2) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n", skey, usname); } else { ret = 0; } } } mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // Event Function //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ void gps_gateway_process(int x, short int y, void *pargs) { unsigned int unFromAddrLen; int nByte = 0; int nSendByte = 0; unsigned char aReqBuffer[512]; unsigned char aRespBuffer[512]; struct sockaddr_in stFromAddr; bson_oid_t oid; bson_t *doc; const bson_t *result; bson_t *query; int i = 0; int command = 0; int monitor_type = 0; char telephone[10]; char text_message[240]; int length; char strBaseinfo[10]; AGPS_info * agps; cell_info * cell; unFromAddrLen = sizeof(stFromAddr); memset(aReqBuffer,0x00,512); memset(aRespBuffer,0x00,512); if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0, (struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1) { printf("error occured while receivingn"); } //proc(); data_frame_head *msg = (data_frame_head *) aReqBuffer; content_gps_up *g = (content_gps_up *)(aReqBuffer + sizeof(data_frame_head)); //异或校验 //异或校验 unsigned char xor = *(aReqBuffer + nByte - 2); if( xor != check_xor(aReqBuffer, nByte - 2)) { return ;//drop down BAD check xor. } switch (msg->cmd) { case CMD_TERMINAL_GPS_UP : //解析位置信息,得到经纬度 fprintf(stderr,"protocal head %x \n",msg->data_head[0] ); fprintf(stderr,"cmd %x \n", msg->cmd ); fprintf(stderr,"length %d \n", ntohs(msg->length) ); //fprintf(stderr,"product_id %u\n",get_product_id(msg->terminal_id)); fprintf(stderr,"product_id %u\n",get_product_id(g->terminal_id)); fprintf(stderr,"flow_id %x \n", ntohs(g->flow_id) ); fprintf(stderr,"data_type %x \n", g->data_type ); fprintf(stderr," time " ); for ( i = 0 ; i< 6 ;i++) fprintf(stderr," %x \n", g->base_info.date_time[i] ); ///////// //经纬度 fprintf(stderr," latitude " ); for ( i = 0 ; i< 4 ;i++) fprintf(stderr,"%x \n", g->base_info.latitude[i] ); fprintf(stderr," longitude " ); for ( i = 0 ; i< 4 ;i++) fprintf(stderr," %x \n", g->base_info.longitude[i] ); ///////// fprintf(stderr,"speed %u \n", ntohs(g->base_info.speed)); fprintf(stderr,"fangxiang %u \n", ntohs(g->base_info.direction)); fprintf(stderr,"gaodu %u\n", ntohs(g->base_info.high)); fprintf(stderr,"定位状态 %c\n", g->base_info.pos_station); fprintf(stderr,"是否应答 %x\n", g->base_info.need_response); if (ntohs(msg->length) > sizeof (content_gps_up)) { fprintf(stderr,"含有附带信息\n"); unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up); fprintf(stderr,"附带信息类型:%x\n",*attach_info); fprintf(stderr,"附带信息长度:%d\n",*(attach_info+1)); switch (*(attach_info)) { case 0x01: break; case 0x02: fprintf(stderr,"基站信息\n"); agps = (AGPS_info * ) ( attach_info + 2); cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info)); fprintf(stderr,"\n国家编码"); fprintf(stderr," %x ", ntohl(agps->country_code) ); fprintf(stderr,"\n运营编码"); fprintf(stderr," %x ", ntohl(agps->mobile_code) ); fprintf(stderr,"\n运基站定位\n"); for ( i = 0 ; i< agps->base_count ;i++){ fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) ); fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) ); fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN ); cell++; } break; case 0x0C: case 0x1E: case 0x1F: case 0x28: case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: break; } } if( g->data_type == 0x80 || g->data_type == 0x82 || g->data_type == 0x8E ) { //保存mongodb unsigned char timegps[15]; unsigned char latitude[10]; unsigned char longitude[10]; double lat; double lng; doc = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_OID (doc, "_id", &oid); BSON_APPEND_INT32 (doc, "id",get_product_id(g->terminal_id)); get_gps_time(g->base_info.date_time, timegps ); BSON_APPEND_UTF8 (doc, "timestamp", timegps); lat = bcd2longitude(g->base_info.latitude, latitude ); lng = bcd2longitude(g->base_info.longitude, longitude ); BSON_APPEND_UTF8 (doc, "latitude", latitude); BSON_APPEND_UTF8 (doc, "longitude", longitude); BSON_APPEND_DOUBLE (doc, "lat", lat); BSON_APPEND_DOUBLE (doc, "lng", lng); BSON_APPEND_INT32 (doc, "speed", ntohs(g->base_info.speed)); BSON_APPEND_INT32 (doc, "direction", ntohs(g->base_info.direction)); BSON_APPEND_INT32 (doc, "high", ntohs(g->base_info.high)); BSON_APPEND_INT32 (doc, "pos_station", g->base_info.pos_station); if (ntohs(msg->length) > sizeof (content_gps_up)) { unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up); switch (*(attach_info)) { case 0x01: break; case 0x02: agps = (AGPS_info * ) ( attach_info + 2); cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info)); fprintf(stderr,"\n国家编码"); fprintf(stderr," %x ", ntohl(agps->country_code) ); BSON_APPEND_INT32 (doc, "country_code", ntohl(agps->country_code)); fprintf(stderr,"\n运营编码"); fprintf(stderr," %x ", ntohl(agps->mobile_code) ); BSON_APPEND_INT32 (doc, "mobile_code", ntohl(agps->mobile_code)); fprintf(stderr,"\n运基站定位\n"); for ( i = 0 ; i< agps->base_count ;i++){ fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) ); fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) ); fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN ); sprintf(strBaseinfo,"lac%d",i); BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->lacN) ); sprintf(strBaseinfo,"cell%d",i); BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->cellN) ); sprintf(strBaseinfo,"signal%d",i); BSON_APPEND_INT32 (doc, strBaseinfo, cell->signalN ); cell++; } break; case 0x0C: case 0x1E: case 0x1F: case 0x28: case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: break; } } if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) { printf ("Insert failed: %s\n", error.message); } bson_destroy (doc); } else if( g->data_type == 0x85 ) { //delete from mongo query = bson_new (); BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id)); if( ! mongoc_collection_remove(collection_cmd, MONGOC_DELETE_NONE, query, NULL, &error)) { fprintf(stderr,"remove command failed \n" ); } bson_destroy (query); } if (g->base_info.need_response == 0x01) { send_22_response( x, aRespBuffer , g->flow_id, xor , g->data_type, stFromAddr, unFromAddrLen ); } break; case CMD_TERMINAL_INFO_UP : fprintf(stderr,"CMD_TERMINAL_INFO_UP\n"); content_info_up * t = (content_info_up *)(aReqBuffer + sizeof(data_frame_head)); fprintf(stderr,"parameter %x \n", t->parameter ); if(t->parameter == 0x01) { sent_time_response( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ); } break; case CMD_VERSION_INFO_UP : fprintf(stderr,"CMD_VERSION_INFO_UP"); break; case CMD_VOICE_UP : fprintf(stderr,"CMD_VOICE_UP"); break; default: break; } //get command from mongo char *str; query = bson_new (); BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id)); cursor = mongoc_collection_find (collection_cmd, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); //while (mongoc_cursor_next (cursor, &doc)) { // mongoc_cursor_next (cursor, &result); while (mongoc_cursor_next (cursor, (const bson_t **) &doc)) { bson_iter_t iter; bson_iter_t sub_iter; str = bson_as_json (doc, NULL); fprintf (stderr, "%s\n", str); bson_free (str); if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "cmd", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); command = (int)bson_iter_int32 (&sub_iter); } if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "parameter", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); strcpy (telephone , bson_iter_utf8 (&sub_iter,&length)); } if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "monitor_type", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); monitor_type = (int)bson_iter_int32 (&sub_iter); } if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "text_message", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); strcpy (text_message , bson_iter_utf8 (&sub_iter,&length)); } } mongoc_cursor_destroy (cursor); bson_destroy (query); //send command to handring if (command == 1) { send_monitor_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ,monitor_type, telephone); } else if (command == 9) { send_sleep_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ); } else if (command == 2) { send_set_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ,text_message); } else{ //为了避免长时间使用网络。在通信6次发送一个休眠指令 if( (msg->cmd == CMD_TERMINAL_GPS_UP) && (check_state(g->terminal_id) == MAX_STANDBY) ) { send_sleep_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ); } } //printf("Function called buffer is %sn",aReqBuffer); g_count++; }
int check_state(unsigned char * terminal_id) { bson_oid_t oid; bson_t *doc = NULL; bson_t *update = NULL; bson_t *query = NULL; int count; int found = 0; found = mongoc_collection_count (collection_state, MONGOC_QUERY_NONE, query, 0, 0, NULL, &error); if (found == 0) { doc = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_INT32 (doc, "id", get_product_id(terminal_id)); BSON_APPEND_INT32 (doc, "count", 0); if (!mongoc_collection_insert (collection_state, MONGOC_INSERT_NONE, doc, NULL, &error)) { printf ("%s\n", error.message); } } query = bson_new (); BSON_APPEND_INT32 (query, "id", get_product_id(terminal_id)); cursor = mongoc_collection_find (collection_state, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); //while (mongoc_cursor_next (cursor, &doc)) { // mongoc_cursor_next (cursor, &result); while (mongoc_cursor_next (cursor, (const bson_t **) &doc)) { bson_iter_t iter; bson_iter_t sub_iter; // str = bson_as_json (doc, NULL); // fprintf (stderr, "%s\n", str); // bson_free (str); if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "count", &sub_iter)) { // fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); // printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); count = (int)bson_iter_int32 (&sub_iter); } } fprintf (stderr,"Found count %d .\n", count); count++; if (count > MAX_STANDBY) { count = 1; update = bson_new (); BSON_APPEND_INT32 (update, "id",get_product_id(terminal_id)); BSON_APPEND_INT32 (update, "count", count); if (!mongoc_collection_update (collection_state, MONGOC_UPDATE_NONE, query, update, NULL, &error)) { printf ("%s\n", error.message); } } else { update = bson_new (); BSON_APPEND_INT32 (update, "id",get_product_id(terminal_id)); BSON_APPEND_INT32 (update, "count", count); if (!mongoc_collection_update (collection_state, MONGOC_UPDATE_NONE, query, update, NULL, &error)) { printf ("%s\n", error.message); } } return count; }