
- 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 remove white spaces (leading and trailing) from string value in MongoDB?
For this, you need to write some code using forEach(). Let us first create a collection with documents −
> db.removingWhiteSpaceDemo.insertOne({"Title":" Introduction to java "}); { "acknowledged" : true, "insertedId" : ObjectId("5cd66f387924bb85b3f4894c") }
Following is the query to display all documents from a collection with the help of find() method −
> db.removingWhiteSpaceDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd66f387924bb85b3f4894c"), "Title" : " Introduction to java " }
Following is the query to remove white spaces (leading and trailing) from string value −
> db.removingWhiteSpaceDemo.find({},{"Title": 1 }).forEach(function(myDocument) { myDocument.Title = myDocument.Title.trim(); db.removingWhiteSpaceDemo.update( { "_id": myDocument._id }, { "$set": { "Title": myDocument.Title } } ); });
Let us check the document once again from the above collection. Following is the query −
> db.removingWhiteSpaceDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd66f387924bb85b3f4894c"), "Title" : "Introduction to java" }
- Related Articles
- Trim a string in Java to remove leading and trailing spaces
- Trim (Remove leading and trailing spaces) a string in C#
- How to remove leading and trailing whitespace from a MySQL field value?
- Java Program to remove leading and trailing whitespace from a string
- Java Program to remove the leading and trailing quotes from a string
- How can I remove the leading and trailing spaces both at once from a string by using MySQL LTRIM() and RTRIM() functions?
- How can I remove the leading and trailing spaces both at once from a string without using MySQL LTRIM() and RTRIM() functions?
- Java Program to remove all white spaces from a String.
- How should I remove all the leading spaces from a string in Swift?
- How to remove leading and trailing whitespace in a MySQL field?
- MySQL query to remove trailing spaces
- Java program to remove all the white spaces from a given string
- Python Pandas – Remove leading and trailing whitespace from more than one column
- How can we eradicate leading and trailing space characters from a string in MySQL?
- How to remove white spaces for textview in Android?

Advertisements