
- 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 replace substring in MongoDB document?
In order to replace substring in MongoDB document, you can use the replace() function. To understand it further, let us create a collection with document. The query to create a collection with document is as follows −
> db.replaceSubstringDemo.insertOne({"WebsiteURL":"www.gogle.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5c76eaf21e9c5dd6f1f78276") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.replaceSubstringDemo.find().pretty();
Output
{ "_id" : ObjectId("5c76eaf21e9c5dd6f1f78276"), "WebsiteURL" : "www.gogle.com" }
Here is the query to replace substring in MongoDB document −
> db.replaceSubstringDemo.find({WebsiteURL:"www.gogle.com"}).forEach(function(url,k){ ... url.WebsiteURL=url.WebsiteURL.replace("www.gogle.com","www.google.com"); ... db.replaceSubstringDemo.save(url) ... });
Let us display the document from a collection once again to verify the replacement has been done or not. The query is as follows −
> db.replaceSubstringDemo.find().pretty();
Output
{ "_id" : ObjectId("5c76eaf21e9c5dd6f1f78276"), "WebsiteURL" : "www.google.com" }
- Related Articles
- How to replace substring in string in TypeScript?
- Replace substring with another substring C++
- How to delete a document in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?
- How to search document in MongoDB by _id
- How to print document value in MongoDB shell?
- How to get embedded data in a MongoDB document?
- How to insert a document with date in MongoDB?
- How to find if element exists in document - MongoDB?
- How to delete particular data in a document in MongoDB?
- How to delete a MongoDB document using Java?
- How to delete document by _id using MongoDB?
- MongoDB $elemMatch to match document
- Get substring in MongoDB aggregate
- How to count number of keys in a MongoDB document?

Advertisements