I'm a new one in ChicagoBoss and Erlang. And I have dumb question:
How can I get last entity from db? Something like User.last
in Rails.
Asked
Active
Viewed 112 times
0

cnaize
- 3,139
- 5
- 27
- 45
-
1Sort descending, limit 1. – WiredPrairie Dec 06 '13 at 11:39
-
Could you please write an example? – cnaize Dec 06 '13 at 12:34
-
Sorry -- I've never used either Erlang or ChicagoBoss. The way to implement `last` in MongoDB is as I described it. – WiredPrairie Dec 06 '13 at 13:20
-
And, having just looked at the docs/source really briefly: https://github.com/mongodb/mongodb-erlang, I'm not sure it's a robust enough driver to actually do the operations I suggested. It hasn't even been updated in a year. – WiredPrairie Dec 06 '13 at 13:27
1 Answers
1
You can use find_last BossDB function:
boss_db:find_last(type).
Query for the last BossRecord of type type
boss_db:find_last(type, [{foo, 'equals', <<"abc">>}, {bar, 'equals', 123}]).
Query for the last BossRecord of type type matching all of the given conditions.
Full API: http://www.chicagoboss.org/api-db.html

P_A
- 1,804
- 1
- 19
- 33
-
From the web page you linked: "Currently Tokyo Tyrant, Mnesia, MySQL, and PostgreSQL are supported." The question was about MongoDB. – WiredPrairie Dec 06 '13 at 20:44
-
From BossDB github page (https://github.com/ChicagoBoss/boss_db): "Supported databases: NEW DynamoDB (experimental), Mnesia, **MongoDB**, MySQL, PostgreSQL, Riak, Tokyo Tyrant". I used BossDB with MongoDB in my project and this is worked fine. – P_A Dec 06 '13 at 22:10
-
From the library: `find_last(Type) -> return_one(find(Type, [], [{limit, 1}, descending])).` Nicely matches what I'd said needed to be done in my first comment. :) Also -- to note, if a field is chosen that isn't sorted already, MongoDB must sort in memory, which may be slow, depending on the size of the collection. – WiredPrairie Dec 06 '13 at 22:19
-
Yes, of course your first comment is right :) I just showed which function does it. – P_A Dec 06 '13 at 22:28