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" ); } }
int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "en_US.UTF-8"); jb = ejdbnew(); if (!ejdbopen(jb, "addressbook", JBOWRITER | JBOCREAT | JBOTRUNC)) { return 1; } //Get or create collection 'contacts' EJCOLL *coll = ejdbcreatecoll(jb, "contacts", NULL); bson bsrec; bson_oid_t oid; //One record bson_init(&bsrec); bson_append_string(&bsrec, "name", "John Travolta"); bson_append_string(&bsrec, "phone", "333-222-333"); bson_append_int(&bsrec, "age", 58); bson_finish(&bsrec); ejdbsavebson(coll, &bsrec, &oid); fprintf(stderr, "\nSaved Travolta"); bson_destroy(&bsrec); //Another record bson_init(&bsrec); bson_append_string(&bsrec, "name", "Bruce Willis"); bson_append_string(&bsrec, "phone", "222-333-222"); bson_append_int(&bsrec, "age", 57); bson_finish(&bsrec); ejdbsavebson(coll, &bsrec, &oid); fprintf(stderr, "\nSaved Bruce Willis"); bson_destroy(&bsrec); //Now select one record. //Query: {'name' : {'$begin' : 'Bru'}} //Name starts with 'Bru' string bson bq1; bson_init_as_query(&bq1); bson_append_start_object(&bq1, "name"); bson_append_string(&bq1, "$begin", "Bru"); bson_append_finish_object(&bq1); bson_finish(&bq1); EJQ *q1 = ejdbcreatequery(jb, &bq1, NULL, 0, NULL); uint32_t count; TCLIST *res = ejdbqryexecute(coll, q1, &count, 0, NULL); fprintf(stderr, "\n\nRecords found: %d\n", count); for (int i = 0; i < TCLISTNUM(res); ++i) { void *bsdata = TCLISTVALPTR(res, i); bson_print_raw((char*) bsdata, 0); } fprintf(stderr, "\n"); //Dispose result set tclistdel(res); //Dispose query ejdbquerydel(q1); bson_destroy(&bq1); //Close database ejdbclose(jb); ejdbdel(jb); getc(stdin); return 0; }
void bson_print_raw( const char *data , int depth ) { bson_iterator i; const char *key; int temp; bson_timestamp_t ts; char oidhex[25]; bson scope; bson_iterator_from_buffer( &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" ); bson_printf( "%s : %d \t " , key , t ); switch ( t ) { case BSON_DOUBLE: printf( "%f" , bson_iterator_double( &i ) ); break; case BSON_STRING: printf( "%s" , bson_iterator_string( &i ) ); break; case BSON_SYMBOL: printf( "SYMBOL: %s" , bson_iterator_string( &i ) ); break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); printf( "%s" , oidhex ); break; case BSON_BOOL: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case BSON_DATE: printf( "%ld" , ( long int )bson_iterator_date( &i ) ); break; case BSON_BINDATA: printf( "BSON_BINDATA" ); break; case BSON_UNDEFINED: printf( "BSON_UNDEFINED" ); break; case BSON_NULL: printf( "BSON_NULL" ); break; case BSON_REGEX: printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) ); break; case BSON_CODE: printf( "BSON_CODE: %s", bson_iterator_code( &i ) ); break; case BSON_CODEWSCOPE: printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); bson_init( &scope ); bson_iterator_code_scope( &i, &scope ); printf( "\n\t SCOPE: " ); bson_print( &scope ); break; case BSON_INT: printf( "%d" , bson_iterator_int( &i ) ); break; case BSON_LONG: printf( "%lld" , ( long long int )bson_iterator_long( &i ) ); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp( &i ); printf( "i: %d, t: %d", ts.i, ts.t ); break; case BSON_OBJECT: case BSON_ARRAY: printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: bson_errprintf( "can't print type : %d\n" , t ); } printf( "\n" ); } }
void bson_print( bson *b ) { bson_print_raw( b->data , 0 ); }
MONGO_EXPORT void bson_print( const bson *b ) { bson_print_raw( b->data , 0 ); }
MONGO_EXPORT void bson_print( const bson *b ) { check_mongo_object( (void*)b ); bson_print_raw( b->data , 0 ); }
void print_record(srld_record *rec) { bson_print_raw(rec->data, 1); }