MonsterWimp757
MonsterWimp757

Reputation: 1217

Mongodb + Node.js - Stream distinct query results

I am using the mongodb driver with Node.js. I have a collection with over 1 million documents and I would like to do something like the following:

var stream = collection.distinct('id',{month:"March"}).stream();
stream.on('data', function(data){console.log(data)})

This returns an error but I would like to do something similar. I would only like to return unique 'id' values from my collection but I am running into memory errors when I try the standard method outlined in their documentation because of the number of results that would populate the returned array.

Is there any other way to get around this?

Any help would be great.

Upvotes: 0

Views: 542

Answers (1)

Asya Kamsky
Asya Kamsky

Reputation: 42352

Use the following aggregation:

db.collection.aggregate({$group:{_id:"$id"}})

If you are on the latest version (2.6.0), you will get a cursor back you can iterate over.

Upvotes: 1

Related Questions