Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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.

2 Answers

0 votes
by (106k points)

To return all the unique value you can use below-mentioned code:-

db.collection.distinct(fields,query)

After using this code you will be able to get the distinct values in your case for NetworkID.

It should be something like this :

Db.collection.distinct('NetworkID')

0 votes
by (107k points)

I think you can use

db.collection.distinct(fields,query)

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...