How can I convert tuple from MongoDB
{'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined}
to proplist
[{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}]
How can I convert tuple from MongoDB
{'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined}
to proplist
[{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}]
Not very efficient, so do not use it on large structures, but really simple:
If order should for some strange reason be important reverse the proplist in the base case of to_proplist1/2.
Assuming you want to basically combine the two consecutive elements of the tuple together, this isn't too hard. You can use
element\2
to pull elements from tuples. Andtuple_size\1
to get the size of the tuple. Here's a couple of ways to handle this:You can use a list comprehension for this:
Or you can zip it:
You can clean up that zip by making a function to handle pulling elements out.
Then calling this function:
You can use bson:fields/1 (https://github.com/mongodb/bson-erlang/blob/master/src/bson.erl#L52). bson is the dependency of mongodb erlang driver
Alternatively you could write a simple function to do it:
This is basically what the list comprehension versions are doing but unravelled into an explicit loop.