see on GitHub

Querying: Distinct

The distinct() method allows you to remove duplicates from the result. Items are compared based on the fields listed in the select section of the query.

// returns sorted list of countries w/o duplicates
const countries = await session
    .query(Order)
    .orderBy("ShipTo.Country")
    .selectFields("ShipTo.Country")
    .distinct()
    .all();
from Orders 
select distinct ShipTo.Country

Paging

Please read the dedicated article about paging through tampered results. This kind of paging is required when using a distinct keyword.

Count

RavenDB supports returning counts when the distinct operation is used.

const numberOfCountries = await session
    .query(Order)
    .selectFields("ShipTo.Country")
    .distinct()
    .count();

Performance

Please keep in mind that this operation might not be efficient for large sets of data due to the need to scan all of the index results in order to find all the unique values.

The same result might be achieved by creating a Map-Reduce index that aggregates data by the field where you want a distinct value of. e.g.

class Order_Countries extends AbstractIndexCreationTask {
    constructor() {
        super();

        this.map = `docs.Orders.Select(o => new {    
                Country = o.ShipTo.Country
            })`;

        this.reduce = `results.GroupBy(r => r.Country)
            .Select(g => new {    
                Country = g.Key
            })`;
    }
}

const numberOfCountries = await session
    .query({ indexName: "Order/Countries" })
    .count();