calling distinct on _id field against a collection is slow

To optimize the code, distinct method should be avoided especially
against _id field. Sort should also be done on the server side to
take advantage of generator to avoid using up memory for large
data set.

Change-Id: Ib417f73e2238a5d9865a2a1a9990e891b0f7a24c
fixes: bug #1204607
fixes: bug #1204609