How to get Distinct Documents from MongoDB using Node.js ? Last Updated : 05 Aug, 2021 Comments Improve Suggest changes Like Article Like Report MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. It stores data in the form of key-value pairs and is a NoSQL database program. The term NoSQL means non-relational. MongoDB module: This module of Node.js is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB. The mongodb.connect() method is used for connecting the MongoDB database which is running on a particular server on your machine. (Refer to this article). We can also use promises in this method in resolve the object contains all the methods and properties required for collection manipulation and in reject, the error occurs during connection. collection.distinct() method of mongodb module is used to finding the distinct documents of a particular database in MongoDB. syntax: collection.distinct(key,callbackfunction) Parameters: This function takes two parameters as mentioned above and described below: The name of the key of the MongoDB database to find distinct values of itCallback function to make this function asynchronous Installing module: npm install mongodb Project structure: Running the server on Local IP: mongod --dbpath=data --bind_ip 127.0.0.1 MongoDB Database: Database:GFG Collection:GFGcollection Database Structure: Index.js JavaScript const MongoClient = require("mongodb"); const url = 'mongodb://localhost:27017/' const database = "GFG"; // Database name MongoClient.connect((url)).then((client) => { // Database reference const connect = client.db(database); // Connect database to connection const collection = connect.collection("GFGcollection"); // class key collection.distinct("class").then((ans) => { // Printing distinct value of class key console.log(ans); }).catch((err) => { console.log(err.Message); }) }) Output: Comment More infoAdvertise with us Next Article How to get Distinct Documents from MongoDB using Node.js ? Z zack_aayush Follow Improve Article Tags : Technical Scripter Web Technologies Node.js MongoDB Technical Scripter 2020 Node.js-Misc +2 More Similar Reads How to Delete a Document From MongoDB Collection using NodeJS? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. This means that MongoDB isnât based on a table-like relational database structure but provides an altogether different mechanism for data storage and retrieval. This stora 4 min read How To Get Data From 2 Different Collections Of MongoDB Using Node.js? To retrieve data from two different collections in MongoDB using Node.js, we typically use the Mongoose library or the native MongoDB driver to establish a connection, query both collections and then combine or process the data as needed. This approach allows you to query multiple collections in par 4 min read How To Query For Documents In MongoDB Using NodeJS? MongoDB is the popular NoSQL database that allows for flexible and scalable data storage. NodeJS and JavaScript runtime built on Chrome's V8 JavaScript engine. It is often used with MongoDB to build powerful and efficient applications. In this article, we will guide you on how to query the documents 4 min read How to Insert a Document into a MongoDB Collection using Node.js? MongoDB, a popular NoSQL database, offers flexibility and scalability for handling data. If you're developing a Node.js application and need to interact with MongoDB, one of the fundamental operations you'll perform is inserting a document into a collection. This article provides a step-by-step guid 5 min read How to fetch single and multiple documents from MongoDb using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo 2 min read Like