
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Unable to implement $addToSet in MongoDB to fetch values of a single field?
The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array.
Let us create a collection with documents −
> db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4cfaef4dcbee04fbbbfc") } > db.demo533.insertOne({"ProjectName":"Online Library Management"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4d02ef4dcbee04fbbbfd") } > db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4d04ef4dcbee04fbbbfe") } > db.demo533.insertOne({"ProjectName":"Online Customer Tracker"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4d0def4dcbee04fbbbff") }
Display all documents from a collection with the help of find() method −
> db.demo533.find();
This will produce the following output −
{ "_id" : ObjectId("5e8b4cfaef4dcbee04fbbbfc"), "ProjectName" : "Online Hospital Management" } { "_id" : ObjectId("5e8b4d02ef4dcbee04fbbbfd"), "ProjectName" : "Online Library Management" } { "_id" : ObjectId("5e8b4d04ef4dcbee04fbbbfe"), "ProjectName" : "Online Hospital Management" } { "_id" : ObjectId("5e8b4d0def4dcbee04fbbbff"), "ProjectName" : "Online Customer Tracker" }
Following is the query to implement $addToSet and fetch the values of field ProjectName −
> db.demo533.aggregate( ... [ ... { ... $group: ... { ... _id:null, ... SetOfProject: { $addToSet: "$ProjectName" } ... } ... } ... ] ... )
This will produce the following output −
{ "_id" : null, "SetOfProject" : [ "Online Customer Tracker", "Online Library Management", "Online Hospital Management" ] }
- Related Articles
- Fetch specific field values in MongoDB
- How to select a single field in MongoDB?
- MongoDB query to fetch array values
- Implement MongoDB $addToSet for an array in an array and append a value
- MySQL query to fetch only a single field on the basis of boolean value in another field
- Use LIKE % to fetch multiple values in a single MySQL query
- Opposite of $addToSet to '$removeFromSet' in MongoDB?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB $addToSet to add a deep nested array of object?
- How to compare field values in MongoDB?
- How to update a single field in a capped collection in MongoDB?
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB query for a single field
- Accessing array values in a MongoDB collection to fetch a specific document
- MongoDB query to limit the returning values of a field?

Advertisements