checkiz / elixir-mongo

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

Converting ObjectId to a hex string? #26

Closed whitfin closed 9 years ago

whitfin commented 9 years ago

I'm extremely new to this, so excuse me if I'm doing something totally wrong. I'm trying to simply retrieve a list of ids currently stored in my collection, and basically end up with a list of hex:

[%{_id: "55abd98c313e46ac825ae48a"}]

Here is my use of this lib:

Mongo.connect! |> Mongo.db("testDb") |> Mongo.Db.collection("testColl") |> Mongo.Collection.find(%{}, %{ "_id" => 1 }) |> Stream.map(&(&1[:_id])) |> Enum.to_list

I was hoping to be able to just throw the response back into Poison, but it appears to be still in BSON. I've looked through the BSON docs, but can't seem to figure out how to convert this into some default type?

Any help would be appreciated :)

jerp commented 9 years ago

ObjectID are wrapped into a struct called Bson.ObjectId

In doing so, it is feasible to used those id in future queries.

If you really want to see the binary values of the id, modify you script like this: Here is my use of this lib:

Mongo.connect! |> Mongo.db("testDb") |> Mongo.Db.collection("testColl") |> Mongo.Collection.find(%{}, %{ "_id" => 1 }) |> Stream.map(&(&1[:_id]  |> Map.get(:oid) )) |> Enum.to_list

This simply get the property :oid of the Bson.ObjectId struct. The only property it has.

whitfin commented 8 years ago

@jerp ah sweet - yeah this was for debugging etc. Thank you!