Mehta
Mehta

Reputation: 392

how can i find distinct field in mongodb through node js

look at the documents of my imageDetails collection:

> db.imageDetails.find()
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e61"), "keyword" : "sachin", "name" : "sachin_1511554882309_1.jpg", "fullpath" : "Download/sachin_1511554882309_1.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e62"), "keyword" : "sachin", "name" : "sachin_1511554882317_2.jpg", "fullpath" : "Download/sachin_1511554882317_2.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e63"), "keyword" : "sachin", "name" : "sachin_1511554882319_3.jpg", "fullpath" : "Download/sachin_1511554882319_3.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e64"), "keyword" : "sachin", "name" : "sachin_1511554882319_4.jpg", "fullpath" : "Download/sachin_1511554882319_4.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e65"), "keyword" : "sachin", "name" : "sachin_1511554882320_5.jpg", "fullpath" : "Download/sachin_1511554882320_5.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e66"), "keyword" : "sachin", "name" : "sachin_1511554882320_6.jpg", "fullpath" : "Download/sachin_1511554882320_6.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e67"), "keyword" : "sachin", "name" : "sachin_1511554882320_7.jpg", "fullpath" : "Download/sachin_1511554882320_7.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e68"), "keyword" : "sachin", "name" : "sachin_1511554882320_8.jpg", "fullpath" : "Download/sachin_1511554882320_8.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e69"), "keyword" : "sachin", "name" : "sachin_1511554882320_9.jpg", "fullpath" : "Download/sachin_1511554882320_9.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6a"), "keyword" : "sachin", "name" : "sachin_1511554882320_10.jpg", "fullpath" : "Download/sachin_1511554882320_10.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6b"), "keyword" : "sachin", "name" : "sachin_1511554882321_11.jpg", "fullpath" : "Download/sachin_1511554882321_11.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6c"), "keyword" : "sachin", "name" : "sachin_1511554882321_12.jpg", "fullpath" : "Download/sachin_1511554882321_12.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6d"), "keyword" : "sachin", "name" : "sachin_1511554882321_13.jpg", "fullpath" : "Download/sachin_1511554882321_13.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6e"), "keyword" : "sachin", "name" : "sachin_1511554882321_14.jpg", "fullpath" : "Download/sachin_1511554882321_14.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6f"), "keyword" : "sachin", "name" : "sachin_1511554882322_15.jpg", "fullpath" : "Download/sachin_1511554882322_15.jpg" }
>

I want the distinct values of the field 'keyword' in nodejs as replacement in mongodb like:

> db.imageDetails.distinct('keyword');
[ "sachin" ]

I tried but didn't find any efficient way in node js mongodb module, so please suggest any method to do so if there is option around aggregation or something more effecient. I am stuck:

MongoClient.connect(url, function(err, db) {
    if (err)
    throw err;
    var cursor = db.collection(collectionName).find(findQuery);
    cursor.each(function(err, doc)

Upvotes: 2

Views: 4400

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93003

You can call distinct on your collection. Here's an example:

db.collection(collectionName).distinct('keyword', function(err, docs) {
  // ...
});

Note that distinct does not return a Cursor. You can either use the callback approach as I've shown above or use a Promise, which can be achieved by not providing a callback. e.g.:

db.collection(collectionName).distinct('keyword')
    .then(docs => {
        // ...
    });

Upvotes: 3

Related Questions