db.collection.distinct()
Drew DiPalma
Hi Folks!
As part of our upcoming re-brand of "Stitch" to "Realm" we'll also be migrating our feedback forum and joining the rest of MongoDB at feedback.mongodb.com, as part of that transition, we'll be shutting down this forum. We're still keeping track of this feedback and all of the related comments/discussions, but all future updates will be made here:
https://feedback.mongodb.com/forums/923521-realm/suggestions/40550596-mongodb-support-for-db-collection-distinct-in-sd
A
Adam Holt
I just ran into this issue.
Something like this is working well in a stitch function for me.
exports = async function(collection, field) {
const mongodb = context.services.get("mongodb-atlas");
const databaseName = context.values.get("DatabaseName");
const collectionDB = mongodb.db(databaseName).collection(collection);
try {
const fromDB = await collectionDB.aggregate([{
$group: {_id: null, uniqueValues: {$addToSet:
$${field}
}}}]).toArray()
const returnResult = fromDB[0].uniqueValues.sort()
return returnResult
} catch (err) {
console.log(JSON.stringify(err));
return err;
}
};
Drew DiPalma
Hi Arnav – Thanks for the feedback, we're keeping a list of MongoDB commands that are are currently not operable with Stitch and will keep this ticket in mind when prioritizing distinct(). In the meantime you should be able to use the aggregation pipeline (similar to here: https://stackoverflow.com/questions/16368638/mongodb-distinct-aggregation) or a Stitch function to get the same data – does this help?