Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-2026

mongoc_gridfs_find_one_with_opts() is not internally optimized for fetching one result

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.7.0
    • Affects Version/s: 1.5.3
    • Component/s: GridFS

      As seen here https://github.com/mongodb/mongo-c-driver/blob/7f1dd3630d6bb4b8d5db4c2b865c328acd170012/src/mongoc/mongoc-gridfs.c#L236 ,
      there is no code that sets the limit = 1 option for the call to _mongoc_gridfs_file_list_new_with_opts().

      As a consequence, the code will always fetch all results (unless explicitly overridden by the user of course) from the server whereas it does in fact use only the first one as a return value. This in turn eliminates any performance gain one could expect from using this function.

      One proposed solution would be to always set the limit = 1 option before calling _mongoc_gridfs_file_list_new_with_opts(). For example:

      ...
      bson_t new_opts;
      bson_init(&new_opts);
      if (opts) bson_copy_to_excluding_noinit(opts, &new_opts, "limit", (char *)NULL);
      BSON_APPEND_INT32(&new_opts, "limit", 1);
      
      list = _mongoc_gridfs_file_list_new_with_opts (gridfs, filter, new_opts);
      ...
      

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            neoxic Arseny Vakhrushev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: