How to get Distinct Documents from MongoDB using Node.js ?
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.
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 it
- Callback function to make this function asynchronous
npm install mongodb
Running the server on Local IP:
mongod --dbpath=data --bind_ip 127.0.0.1