checkiz / elixir-mongo

MongoDB driver for Elixir
MIT License
101 stars 31 forks source link

Is there a way to retrieve all collections? #34

Closed MarkNijhof closed 8 years ago

MarkNijhof commented 8 years ago

I am trying to execute: db.getCollectionNames() using f.ex. Mongo.Db.cmd() but I am not succeeding. Is there a way to directly execute a command against mongodb?

MarkNijhof commented 8 years ago

Found a way, was mostly a difference in command from Mongo shell and internal:

    {:ok, response} = Mongo.connect!
    |> Mongo.db(prefix)
    |> Mongo.Db.cmd_sync(%{listCollections: true})

    {:ok, %{cursor: %{firstBatch: collections} } } = Mongo.Response.cmd(response)

    names = collections |> Enum.map(fn(%{name: name}) -> name end) |> Enum.sort