Skip to content

How to make Mongo.find return a document as a list of tuples to preserve order #204

Answered by zookzook
rhcarvalho asked this question in Q&A
Discussion options

You must be logged in to vote

Hey Rodolfo,

I understand now. Currently, it is not supported. In the encoder, the list of attributes is converted into a map like this:

  defp doc_fields("", acc) do
    acc |> Enum.reverse() |> Enum.into(%{})
  end

You see, the driver has the order of keys, but unfortunately, it gets lost by converting it into a map. I can imagine, that I can add an option like preserve_order: :order to turn the code into:

 defp doc_fields("", acc) do
    acc
    |> Map.new()
    |> Map.put(:order, Enum.map(acc, fn {key, _value} -> key end))
  end

In this case, each document contains an attribute :order that includes a list of keys in the original order. I cannot change the result of the encoder because…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@rhcarvalho
Comment options

Comment options

You must be logged in to vote
1 reply
@rhcarvalho
Comment options

Answer selected by rhcarvalho
Comment options

You must be logged in to vote
1 reply
@rhcarvalho
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants