Using db.collection.distinct() within the aggregation framework

db.collection.distinct is not JSON it’s a command. The syntax ["distinct": "devices", "key": "_id", "query": {"group.$id": 11}] may be legal JSON but it does not represent running a distinct command, it’s just an array with a bunch of values in it. You have two collections and you want to run a pipeline on sensorsDataHistoric but limiting your results to distinct values you get out of another collection (devices).

You need to rewrite the pipeline to be a single query most likely.

Here’s what I don’t understand, you are already doing a $lookup into devices collection, why don’t you limit your $lookup to the correct subset and get rid of that previous distinct?

You tried it but you did it with aggregation on devices and $lookup on sensorsDataHistoric - I’m suggesting doing it the other way around.

By the way, what version is this?

Asya

2 Likes