mongodbmongodb-query

Mongodb find() query : return only unique values (no duplicates)


I have a collection of documents :

{
    "networkID": "myNetwork1",
    "pointID": "point001",
    "param": "param1"
}
{
    "networkID": "myNetwork2",
    "pointID": "point002",
    "param": "param2"
}
{
    "networkID": "myNetwork1",
    "pointID": "point003",
    "param": "param3"
}
...

pointIDs are unique but networkIDs are not.

Is it possible to query Mongodb in such a way that the result will be : [myNetwork1,myNetwork2]

right now I only managed to return [myNetwork1,myNetwork2,myNetwork1]

I need a list of unique networkIDs to populate an autocomplete select2 component. As I may have up to 50K documents I would prefer mongoDb to filter the results at the query level.


Solution

  • I think you can use db.collection.distinct(field, query, options) mongosh method as documented here

    You will be able to get the distinct values in your case for NetworkID. The options argument (and even the query argument) are optional, so you can specify just the field argument as 'NetworkID'

    It should be something like this :

    db.collection.distinct('NetworkID')

    Or as @VagnerWentz asks you can specify a query/filter in second argument using normal mongo syntax. Suppose you have a field called PaymentDate:

    db.collection.distinct('NetworkID', { PaymentDate: { $gt: ... } })

    The distinct shell method only returns a list of distinct values in the field given ('NetworkID'). This means duplicate "rows" are dropped. If you want a "full record" (vs just 'NetworkID'), you need to use a different technique; mongo "aggregation" with the $group operator (akin to a SQL "GROUP BY" technique).

    Then decide which "accumulator operator" to use to pick a row for each distinct 'NetworkID'. Will you use the $first? the $max? Or compile them into a new list with $push/$addToArray ?