int QueryElemMatch(char* key,BSONElem* objElem,BSONElem* query) { bson_iterator oItr; char* k = key; bson obj; bson so; BSONElem tmpVal; BSONElem valElem; BSONElemItr it1; bson_iterator_init(&oItr,&obj); if (!objElem) { BSONElemInit(&tmpVal,BSON_EOO,NULL,0,NULL); return CompareValue(&tmpVal,query); } BSONElemItrInit(&it1,objElem); if (*k) { BSONElem soElem; BSONElem* elemPtr = NULL; bson_iterator sItr; bson b; char* keyEnd = strchr(key,'.'); //printf("Key in new elem '%s','%s'\n",key,keyEnd); if (keyEnd) { *keyEnd = '\0'; keyEnd ++; } else { //printf("No dot \n"); keyEnd = key+strlen(key); } while (BSONElemItrNext(&it1,&tmpVal)) { if (tmpVal.type != BSON_OBJECT) { continue; } bson_iterator_subobject(tmpVal.itr,&b); bson_iterator_init(&sItr,&b); if (bson_find(&sItr,&b,key)) { BSONElemInitFromItr(&soElem,&sItr); elemPtr = &soElem; } if (QueryElemMatch(keyEnd,elemPtr,query)) { return True; } } } else { // end of the string if (query->type == BSON_ARRAY || query->type == BSON_OBJECT) { return CompareValue(objElem,query); } while (BSONElemItrNext(&it1,&tmpVal)) { if (CompareValue(&tmpVal,query)) { return True; } } return False; } return False; }
void test_index_helper( mongo *conn ) { int ret; bson b, out; bson_iterator it; bson_init( &b ); bson_append_int( &b, "foo", -1 ); bson_finish( &b ); mongo_create_index( conn, "test.bar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out ); bson_destroy( &b ); bson_destroy( &out ); bson_init( &b ); bson_append_start_object( &b, "key" ); bson_append_int( &b, "foo", -1 ); bson_append_finish_object( &b ); bson_finish( &b ); ret = mongo_find_one( conn, "test.system.indexes", &b, NULL, &out ); ASSERT( ret == MONGO_OK ); bson_print( &out ); bson_iterator_init( &it, &out ); ASSERT( bson_find( &it, &out, "unique" ) ); ASSERT( bson_find( &it, &out, "sparse" ) ); bson_destroy( &b ); bson_destroy( &out ); }
void bson_print_raw( const char * data , int depth ){ bson_iterator i; const char * key; int temp; char oidhex[25]; bson_iterator_init( &i , data ); while ( bson_iterator_next( &i ) ){ bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); for ( temp=0; temp<=depth; temp++ ) printf( "\t" ); printf( "%s : %d \t " , key , t ); switch ( t ){ case bson_int: printf( "%d" , bson_iterator_int( &i ) ); break; case bson_double: printf( "%f" , bson_iterator_double( &i ) ); break; case bson_bool: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case bson_string: printf( "%s" , bson_iterator_string( &i ) ); break; case bson_null: printf( "null" ); break; case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); printf( "%s" , oidhex ); break; case bson_object: case bson_array: printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: fprintf( stderr , "can't print type : %d\n" , t ); } printf( "\n" ); } }
SEXP mongo_get_databases(SEXP mongo_conn) { mongo* conn = _checkMongo(mongo_conn); bson out; if (mongo_simple_int_command(conn, "admin", "listDatabases", 1, &out) != MONGO_OK) { bson_destroy(&out); return R_NilValue; } bson_iterator it, databases, database; bson_iterator_init(&it, &out); bson_iterator_next(&it); bson_iterator_subiterator(&it, &databases); int count = 0; while (bson_iterator_next(&databases)) { bson_iterator_subiterator(&databases, &database); bson_iterator_next(&database); const char* name = bson_iterator_string(&database); if (strcmp(name, "admin") != 0 && strcmp(name, "local") != 0) count++; } SEXP ret; PROTECT(ret = allocVector(STRSXP, count)); bson_iterator_subiterator(&it, &databases); int i = 0; while (bson_iterator_next(&databases)) { bson_iterator_subiterator(&databases, &database); bson_iterator_next(&database); const char* name = bson_iterator_string(&database); if (strcmp(name, "admin") != 0 && strcmp(name, "local") != 0) SET_STRING_ELT(ret, i++, mkChar(name)); } bson_destroy(&out); UNPROTECT(1); return ret; }
int MCPContentPersonAward::set_basic_info_from_bson_result(bson* bson_out, const char* aggregate_name) { LOGD("[GWJ] %s: start", __FUNCTION__); const char* key; bson_iterator it[1]; stringstream ss; MCPContentPersonAwardRecord* detail = ObjectPoolFactory<MCPContentPersonAwardRecord>::instance().fetch_object(); bson_iterator_init(it, bson_out); while(bson_iterator_next(it)) { ss.clear(); key = bson_iterator_key(it); if(0 == strcmp(key, "priority")) { detail->priority = bson_iterator_string(it); LOGG("[GWJ] %s: set MCPContentPersonAwardRecord.priority[%s]", __FUNCTION__, detail->priority.c_str()); } else if(0 == strcmp(key, "kudou")) { detail->kudou = bson_iterator_int(it); LOGG("[GWJ] %s: set MCPContentPersonAwardRecord.kudou[%d]", __FUNCTION__, detail->kudou; } else if(0 == strcmp(key, "commodity_id"))
bool MongodbObject::GetSubObject(MongodbObject *subObject, const String &fieldname, bool needFinished) { bool found = false; if(0 != objectData->object->finished) { bson_iterator it; bson_iterator_init(&it, objectData->object); bson_type foundType = bson_find(&it, objectData->object, fieldname.c_str()); if(BSON_OBJECT == foundType) { found = true; bson sub; bson_iterator_subobject(&it, &sub); if(needFinished) { subObject->objectData->InitFinished(&sub); } else { subObject->objectData->InitWith(&sub); } } } return found; }
void find_in_array(bson_iterator *it, char *key_ref, char *value_ref, char *key_needed, char *value_needed) { char value_ref_found[MONGO_STRING_LENGTH]; char value_needed_found[MONGO_STRING_LENGTH]; bson_iterator i; while(bson_iterator_next(it)) { switch(bson_iterator_type(it)){ case bson_string: if (strcmp(bson_iterator_key(it), key_ref) == 0) strcpy(value_ref_found, bson_iterator_string(it)); if (strcmp(bson_iterator_key(it), key_needed) == 0) strcpy(value_needed_found, bson_iterator_string(it)); break; case bson_object: case bson_array: bson_iterator_init(&i, bson_iterator_value(it)); find_in_array(&i, key_ref, value_ref, key_needed, value_needed); break; default: break; } } if (strcmp(value_ref_found, value_ref) == 0) strcpy(value_needed, value_needed_found); }
int main(int argc, char *argv[]) { const char * test_server = (argc > 1 ? argv[1] : TEST_SERVER); bson_iterator it[1], it2[1]; bson b[1]; bson sub[1]; bson copy[1]; bson_type type; bson_init( b ); bson_append_string( b, "foo", "hello" ); { bson_append_start_object( b, "o" ); bson_append_string( b, "bar", "goodbye" ); bson_append_finish_object( b ); } bson_iterator_init( it, b ); bson_iterator_next( it ); type = bson_iterator_next( it ); ASSERT( BSON_OBJECT == type ); bson_iterator_subobject_init( it, sub, 0 ); ASSERT( sub->finished == 1 ); bson_iterator_init( it2, sub ); type = bson_iterator_next( it2 ); ASSERT( BSON_STRING == type ); type = bson_iterator_next( it2 ); ASSERT( BSON_EOO == type ); bson_copy( copy, sub ); ASSERT( 1 == copy->finished ); ASSERT( 0 == copy->stackPos ); ASSERT( 0 == copy->err ); bson_destroy( copy ); bson_destroy( b ); return 0; }
bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) { bson_iterator_init( it, (bson *)obj ); while( bson_iterator_next( it ) ) { if ( strcmp( name, bson_iterator_key( it ) ) == 0 ) break; } return bson_iterator_type( it ); }
void MongodbClient::ReadData(KeyedArchive* archive, void* bsonObj) { if((!archive) || (!bsonObj)) return; bson_iterator it; bson_iterator_init(&it, (bson*)bsonObj); while (bson_iterator_next(&it)) { String key = String(bson_iterator_key(&it)); bson_type type = bson_iterator_type(&it); if(key == "_id") continue; // ignore _id switch (type) { case BSON_STRING: archive->SetString(key, String(bson_iterator_string(&it))); break; case BSON_INT: archive->SetInt32(key, bson_iterator_int(&it)); break; case BSON_LONG: archive->SetInt32(key, (int32)bson_iterator_long(&it)); break; case BSON_DOUBLE: archive->SetFloat(key, (float32)bson_iterator_double(&it)); break; case BSON_OBJECT: { bson sub; bson_iterator_subobject(&it, &sub); KeyedArchive* subArchive = new KeyedArchive(); ReadData(subArchive, &sub); archive->SetArchive(key, subArchive); SafeRelease(subArchive); break; } case BSON_OID: //TODO: add 12-bytes array //bson_append_oid(object, key, bson_iterator_oid(&it)); break; default: DVASSERT(false); Logger::Error("[MongodbUpdateObject::ReadData] Not implemented type: %d", type); break; } } }
bool bson_cmp( const bson *a, const bson *b ) { assert( a ); assert( b ); bson_iterator *a_it, *b_it; bool equal; size_t a_keys_count, b_keys_count; equal = true; a_it = malloc( sizeof( bson_iterator ) ); b_it = malloc( sizeof( bson_iterator ) ); bson_iterator_init( a_it, a ); bson_iterator_init( b_it, b ); a_keys_count = bson_keypars_count( a ); b_keys_count = bson_keypars_count( b ); if( a_keys_count == b_keys_count ) { while( bson_iterator_next( a_it ) ) { bson_type a_key_type = bson_iterator_type( a_it ); const char *a_key = bson_iterator_key( a_it ); bson_type b_key_type = bson_find( b_it, b, a_key ); if( b_key_type == BSON_EOO || a_key_type != b_key_type || !bson_values_are_equal( a_key_type, a_it, b_it ) ) { equal = false; break; } } } else { equal = false; } free( a_it ); free( b_it ); return equal; }
MONGO_EXPORT bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) { check_mongo_object( (void*)obj ); check_mongo_object( it ); bson_iterator_init( it, (bson *)obj ); while( bson_iterator_next( it ) ) { if ( strcmp( name, bson_iterator_key( it ) ) == 0 ) break; } return bson_iterator_type( it ); }
//------------------------------------------------------------------------------ int main( int argc, char * argv[] ){ mongo conn; if( mongo_client( &conn , TEST_SERVER, TEST_PORT ) != MONGO_OK ) { std::cout << "failed to connect\n"; return EXIT_FAILURE; } mongo_cursor cursor; mongo_cursor_init( &cursor, &conn, "test.test" ); char hex_oid[25]; while( mongo_cursor_next( &cursor ) == MONGO_OK ) { std::cout << "row:\n"; bson_iterator it; bson_iterator_init( &it, mongo_cursor_bson( &cursor ) ); while( bson_iterator_next( &it ) ) { std::cout << " " << bson_iterator_key( &it ) << " = "; switch( bson_iterator_type( &it ) ) { case BSON_DOUBLE: std::cout << "(double) " << bson_iterator_double( &it ) << std::endl; break; case BSON_INT: std::cout << "(int) " << bson_iterator_int( &it ) << std::endl; break; case BSON_STRING: std::cout << "(string) \"" << bson_iterator_string( &it ) << "\"\n"; break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &it ), hex_oid ); std::cout << "(oid) \"" << hex_oid << "\"\n"; break; case BSON_OBJECT: std::cout << "(subobject) {...}\n"; break; case BSON_ARRAY: std::cout << "(array) [...]\n"; break; case BSON_TIMESTAMP: std::cout << "(timestamp) [...]\n"; break; default: std::cout << "(type " << bson_iterator_type( &it ) << std::endl; break; } } std::cout << std::endl; } mongo_disconnect( &conn ); return EXIT_SUCCESS; }
int test_bson_iterator( void ) { bson b[1]; bson_iterator i[1]; bson_iterator_init( i, bson_empty( b ) ); bson_iterator_next( i ); bson_iterator_type( i ); bson_find( i, bson_empty( b ), "foo" ); return 0; }
yajl_gen bson_to_yajl(bson *b) { bson_iterator it[1]; bson_iterator_init( it, b ); yajl_gen g = yajl_gen_alloc( NULL ); yajl_gen_config( g, yajl_gen_beautify, 1 ); json_from_bson_object( &g, it ); return g; }
void MongodbObject::EnableForEdit() { bson *newObject = new bson(); bson_init(newObject); bson_iterator it; bson_iterator_init(&it, objectData->object); while (bson_iterator_next(&it)) { const char * key = bson_iterator_key(&it); bson_type type = bson_iterator_type(&it); switch (type) { case BSON_STRING: bson_append_string(newObject, key, bson_iterator_string(&it)); break; case BSON_INT: bson_append_int(newObject, key, bson_iterator_int(&it)); break; case BSON_LONG: bson_append_long(newObject, key, bson_iterator_long(&it)); break; case BSON_DOUBLE: bson_append_double(newObject, key, bson_iterator_double(&it)); break; case BSON_OBJECT: bson sub; bson_iterator_subobject(&it, &sub); bson_append_bson(newObject, key, &sub); break; case BSON_OID: bson_append_oid(newObject, key, bson_iterator_oid(&it)); break; default: break; } } bson_destroy(objectData->object); SafeDelete(objectData->object); objectData->object = newObject; }
void testSaveLoad() { CU_ASSERT_PTR_NOT_NULL_FATAL(jb); bson_oid_t oid; EJCOLL *ccoll = ejdbcreatecoll(jb, "contacts", NULL); CU_ASSERT_PTR_NOT_NULL(ccoll); //Save record bson a1; bson_init(&a1); bson_append_string(&a1, "name", "Петров Петр"); bson_append_string(&a1, "phone", "333-222-333"); bson_append_int(&a1, "age", 33); bson_append_long(&a1, "longage", 0xFFFFFFFFFF01LL); bson_append_double(&a1, "doubleage", 0.333333); bson_finish(&a1); ejdbsavebson(ccoll, &a1, &oid); bson_destroy(&a1); bson *lbson = ejdbloadbson(ccoll, &oid); CU_ASSERT_PTR_NOT_NULL(lbson); bson_iterator it1; bson_iterator_init(&it1, lbson); int btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_OID); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_STRING); CU_ASSERT(!strcmp("name", bson_iterator_key(&it1))); CU_ASSERT(!strcmp("Петров Петр", bson_iterator_string(&it1))); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_STRING); CU_ASSERT(!strcmp("phone", bson_iterator_key(&it1))); CU_ASSERT(!strcmp("333-222-333", bson_iterator_string(&it1))); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_INT); CU_ASSERT(!strcmp("age", bson_iterator_key(&it1))); CU_ASSERT(33 == bson_iterator_int(&it1)); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_LONG); CU_ASSERT(!strcmp("longage", bson_iterator_key(&it1))); CU_ASSERT(0xFFFFFFFFFF01LL == bson_iterator_long(&it1)); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_DOUBLE); CU_ASSERT(!strcmp("doubleage", bson_iterator_key(&it1))); CU_ASSERT_DOUBLE_EQUAL(bson_iterator_double(&it1), 0.3, 0.1); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_EOO); bson_del(lbson); }
int read_inode(const bson * doc, struct inode * out) { bson_iterator i, sub; bson_type bt; const char * key; bson_iterator_init(&i, doc); while((bt = bson_iterator_next(&i)) > 0) { key = bson_iterator_key(&i); if(strcmp(key, "_id") == 0) memcpy(&out->oid, bson_iterator_oid(&i), sizeof(bson_oid_t)); else if(strcmp(key, "mode") == 0) out->mode = bson_iterator_int(&i); else if(strcmp(key, "owner") == 0) out->owner = bson_iterator_long(&i); else if(strcmp(key, "group") == 0) out->group = bson_iterator_long(&i); else if(strcmp(key, "size") == 0) out->size = bson_iterator_long(&i); else if(strcmp(key, "created") == 0) out->created = bson_iterator_time_t(&i); else if(strcmp(key, "modified") == 0) out->modified = bson_iterator_time_t(&i); else if(strcmp(key, "data") == 0) { out->datalen = bson_iterator_string_len(&i); out->data = malloc(out->datalen + 1); strcpy(out->data, bson_iterator_string(&i)); } else if(strcmp(key, "dirents") == 0) { while(out->dirents) { struct dirent * next = out->dirents->next; free(out->dirents); out->dirents = next; } bson_iterator_subiterator(&i, &sub); while((bt = bson_iterator_next(&sub)) > 0) { int len = bson_iterator_string_len(&sub); struct dirent * cde = malloc(sizeof(struct dirent) + len); if(!cde) return -ENOMEM; strcpy(cde->path, bson_iterator_string(&sub)); cde->len = bson_iterator_string_len(&sub); cde->next = out->dirents; out->dirents = cde; out->direntcount++; } } } return 0; }
void InitWith(bson *obj) { bson_iterator it; bson_iterator_init(&it, obj); while (bson_iterator_next(&it)) { const char * key = bson_iterator_key(&it); bson_type type = bson_iterator_type(&it); switch (type) { case BSON_STRING: bson_append_string(object, key, bson_iterator_string(&it)); break; case BSON_INT: bson_append_int(object, key, bson_iterator_int(&it)); break; case BSON_LONG: bson_append_long(object, key, bson_iterator_long(&it)); break; case BSON_DOUBLE: bson_append_double(object, key, bson_iterator_double(&it)); break; case BSON_OBJECT: { bson sub; bson_iterator_subobject(&it, &sub); bson_append_bson(object, key, &sub); break; } case BSON_OID: bson_append_oid(object, key, bson_iterator_oid(&it)); break; default: DVASSERT(false); Logger::Error("[MongodbObjectInternalData::InitWith] Not implemented type: %d", type); break; } } }
int srld_find(bson_iterator *i, int keyc, char **keyv, char *data, int depth) { bson_iterator_init(i, data); while(bson_iterator_next(i)){ if (strcmp(keyv[depth], bson_iterator_key(i)) == 0) { //printf("%s == %s, depth %d\n", keyv[depth], bson_iterator_key(i), depth); if (depth == keyc-1) { return 1; } else { return srld_find(i, keyc, keyv, (char *)bson_iterator_value(i), depth+1); } } } return 0; }
size_t bson_keypars_count( const bson *a ) { assert( a ); size_t i; bson_iterator *a_it; a_it = malloc( sizeof( bson_iterator ) ); bson_iterator_init( a_it, a ); for( i = 0; bson_iterator_next( a_it ); i++ ); free( a_it ); return i; }
uint8_t QueryObjMatch(bson* obj,bson* query) { bson_iterator oItr,qItr; bson_type o,q; bson_iterator_init(&oItr,obj); bson_iterator_init(&qItr,query); while ((o=bson_iterator_next(&oItr))) { BSONElem oVal,qVal; int cVal; q = bson_find(&qItr,query,bson_iterator_key(&oItr)); if (!q) { return False; } if ( (o == BSON_OBJECT && q != BSON_OBJECT ) || (q == BSON_OBJECT && o != BSON_OBJECT ) ) { return False; } if ( o == q && o == BSON_OBJECT) { bson o1,q1; bson_iterator_subobject(&oItr,&o1); bson_iterator_subobject(&qItr,&q1); return QueryObjMatch(&o1,&q1); } BSONElemInitFromItr(&oVal,&oItr); BSONElemInitFromItr(&qVal,&qItr); if (CompareElem(&oVal,&qVal,&cVal)) { return False; } if (cVal) { return False; } } return True; }
String MongodbObject::GetString(const String &fieldname) { String retValue = String(""); bson_iterator it; bson_iterator_init(&it, objectData->object); bson_iterator foundIt; bool found = objectData->FindField(&it, &foundIt, fieldname, true); if(found) { retValue = bson_iterator_string(&foundIt); } return retValue; }
bool MongodbObject::GetData(const String &fieldname, uint8 *outData, int32 dataSize) { bson_iterator it; bson_iterator_init(&it, objectData->object); bson_iterator foundIt; bool found = objectData->FindField(&it, &foundIt, fieldname, true); if(found) { uint8 *binaryData = (uint8 *)bson_iterator_bin_data(&foundIt); Memcpy(outData, binaryData, dataSize); found = true; } return found; }
double MongodbObject::GetDouble(const String &fieldname) { double retValue = 0; bson_iterator it; bson_iterator_init(&it, objectData->object); bson_iterator foundIt; bool found = objectData->FindField(&it, &foundIt, fieldname, true); if(found) { retValue = bson_iterator_double(&foundIt); } return retValue; }
static void mongo_replset_check_seed( mongo_connection* conn ) { bson out; bson hosts; const char* data; bson_iterator it; bson_iterator it_sub; const char* host_string; mongo_host_port *host_port = NULL; out.data = NULL; out.owned = 1; hosts.data = NULL; hosts.owned = 1; if( mongo_simple_int_command(conn, "admin", "ismaster", 1, &out) == MONGO_OK ) { if( bson_find( &it, &out, "hosts" ) ) { data = bson_iterator_value( &it ); bson_iterator_init( &it_sub, data ); /* Iterate over host list, adding each host to the * connection's host list. */ while( bson_iterator_next( &it_sub ) ) { host_string = bson_iterator_string( &it_sub ); host_port = bson_malloc( sizeof( mongo_host_port ) ); mongo_parse_host( host_string, host_port ); if( host_port ) { mongo_replset_add_node( &conn->replset->hosts, host_port->host, host_port->port ); free( host_port ); host_port = NULL; } } } } bson_destroy( &out ); bson_destroy( &hosts ); mongo_close_socket( conn->sock ); conn->sock = 0; conn->connected = 0; }
int find_radius_options(rlm_mongo_t *data, char *username, char *mac, char *password) { bson_buffer bb; bson query; bson field; bson result; bson_buffer_init(&bb); bson_append_string(&bb, data->search_field, username); if (strcmp(data->mac_field, "") != 0) { bson_append_string(&bb, data->mac_field, mac); } if (strcmp(data->enable_field, "") != 0) { bson_append_bool(&bb, data->enable_field, 1); } bson_from_buffer(&query, &bb); bson_buffer_destroy(&bb); bson_empty(&field); bson_empty(&result); MONGO_TRY{ if (mongo_find_one(conn, data->base, &query, &field, &result) == 0) { return 0; } }MONGO_CATCH{ mongo_start(data); return 0; } bson_iterator it; bson_iterator_init(&it, result.data); find_in_array(&it, data->username_field, username, data->password_field, password); bson_destroy(&result); return 1; }
void json_from_bson_type(bson_type t, yajl_gen *g, bson_iterator *it) { switch( t ) { case BSON_ARRAY: { bson_iterator subi[1]; bson_iterator_subiterator( it, subi ); json_from_bson_array( g, subi ); break; } case BSON_OBJECT: { bson sub[1]; bson_iterator_subobject( it, sub ); bson_iterator subi[1]; bson_iterator_init( subi, sub ); json_from_bson_object( g, subi ); break; } case BSON_DOUBLE: json_from_bson_double( g, it ); break; case BSON_STRING: json_from_bson_string( g, it ); break; case BSON_BOOL: json_from_bson_bool( g, it ); break; case BSON_NULL: json_from_bson_null( g ); break; case BSON_INT: json_from_bson_int( g, it ); break; case BSON_LONG: json_from_bson_long( g, it ); break; default: break; } }
int mongo_create_index(mongo_connection * conn, const char * ns, bson * key, int options, bson * out){ bson_buffer bb; bson b; bson_iterator it; char name[255] = {'_'}; int i = 1; char idxns[1024]; bson_iterator_init(&it, key->data); while(i < 255 && bson_iterator_next(&it)){ strncpy(name + i, bson_iterator_key(&it), 255 - i); i += strlen(bson_iterator_key(&it)); } name[254] = '\0'; bson_buffer_init(&bb); bson_append_bson(&bb, "key", key); bson_append_string(&bb, "ns", ns); bson_append_string(&bb, "name", name); if (options & MONGO_INDEX_UNIQUE) bson_append_bool(&bb, "unique", 1); if (options & MONGO_INDEX_DROP_DUPS) bson_append_bool(&bb, "dropDups", 1); if (options & MONGO_INDEX_BACKGROUND) bson_append_bool(&bb, "background", 1); if (options & MONGO_INDEX_SPARSE) bson_append_bool(&bb, "sparse", 1); bson_from_buffer(&b, &bb); strncpy(idxns, ns, 1024-16); strcpy(strchr(idxns, '.'), ".system.indexes"); mongo_insert(conn, idxns, &b); bson_destroy(&b); *strchr(idxns, '.') = '\0'; /* just db not ns */ return mongo_cmd_get_last_error(conn, idxns, out); }
clsSiteData::clsSiteData(int id, const bson *info) { m_ID = id; bson_iterator it[1]; bson_iterator_init(it, info); while (bson_iterator_next(it)) { if (strcmp("Name", bson_iterator_key(it)) == 0) m_Name = bson_iterator_string(it); //else if(strcmp("AREA", bson_iterator_key(it)) == 0) // m_Area = (float)bson_iterator_double(it); else if (strcmp("LocalX", bson_iterator_key(it)) == 0) m_XPR = (float) bson_iterator_double(it); else if (strcmp("LocalY", bson_iterator_key(it)) == 0) m_YPR = (float) bson_iterator_double(it); else if (strcmp("Lat", bson_iterator_key(it)) == 0) m_Latitude = (float) bson_iterator_double(it); else if (strcmp("Long", bson_iterator_key(it)) == 0) m_Longitude = (float) bson_iterator_double(it); else if (strcmp("Elevation", bson_iterator_key(it)) == 0) m_Elevation = (float) bson_iterator_double(it); } }