Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty list for sort gives Mongo.Error #186

Open
ksmithut opened this issue May 9, 2023 · 1 comment
Open

Empty list for sort gives Mongo.Error #186

ksmithut opened this issue May 9, 2023 · 1 comment

Comments

@ksmithut
Copy link
Contributor

ksmithut commented May 9, 2023

One is able to sort with a keyword list, but if you sort with an empty keyword list (aka empty list), it gives an error.

Reproduction steps:

{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/my-database")
%Mongo.Stream{} = Mongo.find(conn, "test", %{}, sort: [])

This will create a match error:

** (MatchError) no match of right hand side value: {:error, %Mongo.Error{code: 14, error_labels: [], fail_command: false, host: nil, message: "Expected field sortto be of type object", not_writable_primary_or_recovering: false, resumable: false, retryable_reads: false, retryable_writes: false}}

I've tracked the code down to this function where it reduces over the list. When it's a list of key/value tuples, it will create an iodata encoded bson object, but if it's empty, the io data will be "". I'm not sure what the right answer is here. With elixir, you can't determine whether [] is meant to be an empty list or empty keyword list.

@zookzook
Copy link
Owner

zookzook commented May 9, 2023

Hey ksmithut!

The :sort keyword belongs to the option of the find function. First, you can just avoid specifying an empty list for the sort option. But we can extend the filter_nils function to handle this case (sort: []) as well.

cmd = [
      find: coll,
      filter: filter,
      limit: opts[:limit],
      hint: opts[:hint],
      singleBatch: opts[:single_batch],
      readConcern: opts[:read_concern],
      max: opts[:max],
      min: opts[:min],
      collation: opts[:collation],
      returnKey: opts[:return_key],
      showRecordId: opts[:show_record_id],
      tailable: opts[:tailable],
      oplogReplay: opts[:oplog_replay],
      noCursorTimeout: opts[:no_cursor_timeout],
      awaitData: opts[:await_data],
      batchSize: opts[:batch_size],
      projection: opts[:projection],
      comment: opts[:comment],
      maxTimeMS: opts[:max_time],
      skip: opts[:skip],
      sort: opts[:sort]
    ]

    cmd = filter_nils(cmd)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants