1
votes

I used this https://github.com/ankhers/mongodb driver to run queries on my Elixir application.

The doc specifies that the Mongo.find(...) function returns specified fields with projection option. But, when I try to pass the required field in projection, I get the following error:

** (Protocol.UndefinedError) protocol Enumerable not implemented for "ques_text". This protocol is implemented for: DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Mongo.AggregationCursor, Mongo.Cursor, Mongo.SinglyCursor, Postgrex.Stream, Range, Stream, Timex.Interval

My query looks like this:

Mongo.find(:mongo, "questions", %{"no_of_options": 4}, [projection: "ques_text", limit: 1])

And I can see the ques_text field under select field of %Mongo.Cursor.

Is that a wrong way to pass projection option? I am new to Elixir.

1
Can you try this: [projection: %{"ques_text" => 1}, limit: 1]?Dogbert
@Dogbert. It works. Thanks.Rahul Sharma

1 Answers

2
votes

projection should be a map with the key being the field name and the value being one of those specified here. In this case, you probably want:

projection: %{"ques_text" => 1}

This will select only the ques_text field in each document.