
- 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 search for a record (field) and then delete it in MongoDB?
To search for a field, use $exists and to delete it, use $unset. The $unset operator in MongoDB deletes a particular field.
Let us create a collection with documents −
> db.demo562.insertOne({"Name":"Chris","Age":21});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f4ae854b4472ed3e8e872") } > db.demo562.insertOne({"Age":20});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f4ae954b4472ed3e8e873") } > db.demo562.insertOne({"Name":"David","Age":23});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f4aea54b4472ed3e8e874") }
Display all documents from a collection with the help of find() method −
> db.demo562.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f4ae854b4472ed3e8e872"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e8f4ae954b4472ed3e8e873"), "Age" : 20 } { "_id" : ObjectId("5e8f4aea54b4472ed3e8e874"), "Name" : "David", "Age" : 23 }
Following is the query to search for a record and then delete it −
> db.demo562.update( {Name:{$exists:true} },{ $unset: {"Name":""}},{multi:true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo562.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f4b6654b4472ed3e8e875"), "Age" : 21 } { "_id" : ObjectId("5e8f4b6754b4472ed3e8e876"), "Age" : 20 } { "_id" : ObjectId("5e8f4b6854b4472ed3e8e877"), "Age" : 23 }
- Related Articles
- Delete a field and value in MongoDB?
- How to search for string or number in a field with MongoDB?
- How do I add a field to existing record in MongoDB?
- Search a sub-field on MongoDB?
- Search by specific field in MongoDB
- MongoDB query to search for string like “@email” in the field values
- How to search for a date in MySQL timestamp field?
- How to delete a single record in Laravel 5?
- Delete all elements in an array field in MongoDB?
- How to delete a document in MongoDB?
- How to search a record by date in MySQL table?
- Understanding the find() method to search for a specific record in JavaScript?
- How to find a record by _id in MongoDB?
- Delete specific record from an array nested within another array in MongoDB?
- How to delete everything in a MongoDB database?

Advertisements