
- 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
How to exclude array type field value in MongoDB?
To exclude array type field value, use delete() in MongoDB. Let us create a collection with documents −
> db.demo464.insertOne( ... { ... ... "id" : "101", ... "details": [ ... { ... Name:"Chris" ... }, ... { ... Name:"David" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e7f8832cb66ccba22cc9dda") }
Display all documents from a collection with the help of find() method −
> db.demo464.find();
This will produce the following output −
{ "_id" : ObjectId("5e7f8832cb66ccba22cc9dda"), "id" : "101", "details" : [ { "Name" : "Chris" }, { "Name" : "David" } ] }
Following is the query to exclude array type field values −
> db.demo464.find({id: "101"}).forEach(function(mongoDocument) { ... ... var details = mongoDocument.details; ... for(var j = 0; j − details.length; ++j) { ... var array = details[j]; ... delete (array["Name"]); ... ... } ... db.demo464.save(mongoDocument); ... });
Display all documents from a collection with the help of find() method −
> db.demo464.find();
This will produce the following output −
{ "_id" : ObjectId("5e7f8832cb66ccba22cc9dda"), "id" : "101", "details" : [ { }, { } ] }
- Related Articles
- MongoDB query to exclude if id is equal to a document field array value
- Replace an array field value with MongoDB?
- How to change the type of a field in MongoDB?
- Reverse array field in MongoDB?
- How to return only value of a field in MongoDB?
- How to convert string type value to array type in JavaScript?
- How to exclude a field in Gson during serialization in Java?
- Find and replace NumberLong type field in MongoDB?
- MongoDB slice array in populated field?
- How to project specific elements in an array field with MongoDB?
- Update MongoDB field using value of another field?
- Delete a field and value in MongoDB?
- How to exclude a field from JSON using @Expose annotation in Java?
- Sorting field value (FirstName) for MongoDB?
- How to add a field with static value to MongoDB find query?

Advertisements