javascript - Mongo db alphabetical sorting in coffeescript -
i have collection of users, , insert them select box viewable user. wanting sort collection users name , pass on.
i grabbing roomname , returning roomname.active_users. populates select box doesn't sort them.
template.room_active_users.getactiveusers = (roomname) -> users = rooms.findone({room_name: roomname}).active_users
i have tried doing
users = rooms.findone({room_name: roomname}).active_users.profile.name
but doesn't work.
currently inside collection when console.log(users):
[object, null, object] // says there 2 users in room 0: object _id: "ryynj48gcgd25fwhm" profile: avatar_url: bio: blog: . . . login: name: jerry 1: null 2: object _id: "hg4mxetjdxfdtpjje" profile: avatar_url: bio: blog: . . . login: name: bob
i trying find way access users' names , sort them way can't call
.active_users.profile.name
it gives error:
exception deps recompute function: typeerror: cannot read property 'name' of undefined
i figured have write type of sort function this, examples i've searched have turned out not work, , none specific enough fit case.
any appreciated, thanks.
you can sort , collect in database or in code. db gonna better @ sort of thing , there's not of reason pull fields when need one. if want in code, i'd use lodash's pluck:
# load users collection lodash can multiple operations chained # compact removes nulls # pluck gets key collection of objects # value returns value chain # _(rooms.findone({room_name: roomname})).compact().pluck('name').value()
is findone not asynch? seems should be...
it looks findone accepts projection parameter, in case you'd need pass in so:
rooms.findone({room_name: roomname}, {'name':1})
Comments
Post a Comment