

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 convert string type value to array type in JavaScript?
- Update MongoDB field using value of another field?
- Find and replace NumberLong type field in MongoDB?
- How to return only value of a field in MongoDB?
- How to exclude a field in Gson during serialization in Java?
- Sorting field value (FirstName) for MongoDB?
- Delete a field and value in MongoDB?
- MongoDB slice array in populated field?
- How to exclude a field from JSON using @Expose annotation in Java?
- How to project specific elements in an array field with MongoDB?
- MongoDB aggregation to fetch documents with specific field value?
Advertisements