I need to take only unique values from collection. For example:
const userID = `user1`;
const users = await Chat
.find({'$or': [{to: userID}, {from: userID}]})
.select(`-_id to from`)
.lean();
// users will be like:
[
{from: `user1`, to: `user2`},
{from: `user1`, to: `user2`},
{from: `user1`, to: `user2`},
{from: `user2`, to: `user1`},
{from: `user3`, to: `user1`},
// ... 10089 more items
];
// and I want this in result
const result = [`user2`, `user3`]; // exclude current user too
I know that I can do this using JS. I can create an array from users and run new Set(), but it will be slow. Can Mongoose do this instead of me?