@@ -586,15 +586,13 @@ mongoc_database_has_collection (mongoc_database_t *database,
586
586
const char * name ,
587
587
bson_error_t * error )
588
588
{
589
- mongoc_collection_t * collection ;
590
- mongoc_read_prefs_t * read_prefs ;
591
- mongoc_cursor_t * cursor ;
592
- const bson_t * doc ;
593
- bson_iter_t iter ;
589
+ bson_t * infos = NULL ;
590
+ bson_iter_t info_iter ;
591
+ bson_iter_t col_array_iter ;
592
+ bson_iter_t col_iter ;
594
593
bool ret = false;
595
594
const char * cur_name ;
596
- bson_t q = BSON_INITIALIZER ;
597
- char ns [140 ];
595
+ bson_t filter = BSON_INITIALIZER ;
598
596
599
597
ENTRY ;
600
598
@@ -605,34 +603,38 @@ mongoc_database_has_collection (mongoc_database_t *database,
605
603
memset (error , 0 , sizeof * error );
606
604
}
607
605
608
- bson_snprintf ( ns , sizeof ns , "%s.%s" , database -> name , name );
606
+ BSON_APPEND_UTF8 ( & filter , " name" , name );
609
607
610
- read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY );
611
- collection = mongoc_client_get_collection (database -> client ,
612
- database -> name ,
613
- "system.namespaces" );
614
- cursor = mongoc_collection_find (collection , MONGOC_QUERY_NONE , 0 , 0 , 0 , & q ,
615
- NULL , read_prefs );
616
-
617
- while (!mongoc_cursor_error (cursor , error ) &&
618
- mongoc_cursor_more (cursor )) {
619
- while (mongoc_cursor_next (cursor , & doc ) &&
620
- bson_iter_init_find (& iter , doc , "name" ) &&
621
- BSON_ITER_HOLDS_UTF8 (& iter )) {
622
- cur_name = bson_iter_utf8 (& iter , NULL );
623
- if (!strcmp (cur_name , ns )) {
624
- ret = true;
625
- GOTO (cleanup );
608
+ infos = mongoc_database_get_collection_info (database , & filter , error );
609
+
610
+ if (!infos ||
611
+ (error &&
612
+ ((error -> domain != 0 ) ||
613
+ (error -> code != 0 )))) {
614
+ return ret ;
615
+ }
616
+
617
+ if (bson_iter_init_find (& info_iter , infos , "collections" ) &&
618
+ BSON_ITER_HOLDS_ARRAY (& info_iter ) &&
619
+ bson_iter_recurse (& info_iter , & col_array_iter )) {
620
+ while (bson_iter_next (& col_array_iter )) {
621
+ if (BSON_ITER_HOLDS_DOCUMENT (& col_array_iter ) &&
622
+ bson_iter_recurse (& col_array_iter , & col_iter ) &&
623
+ bson_iter_find (& col_iter , "name" ) &&
624
+ BSON_ITER_HOLDS_UTF8 (& col_iter ) &&
625
+ (cur_name = bson_iter_utf8 (& col_iter , NULL ))) {
626
+ if (!strcmp (cur_name , name )) {
627
+ ret = true;
628
+ GOTO (cleanup );
629
+ }
626
630
}
627
631
}
628
632
}
629
633
630
634
cleanup :
631
- mongoc_cursor_destroy (cursor );
632
- mongoc_collection_destroy (collection );
633
- mongoc_read_prefs_destroy (read_prefs );
635
+ bson_free (infos );
634
636
635
- RETURN (ret );
637
+ RETURN (ret );
636
638
}
637
639
638
640
/* Uses old way of querying system.namespaces. */
0 commit comments