
- 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 trim spaces from field in MongoDB query?
To trim spaces from field, use $trim in MongoDB. Let us create a collection with documents −
> db.demo217.insertOne({"FullName":" Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5d1e03d395bdc213470f") } > db.demo217.insertOne({"FullName":" David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5d2503d395bdc2134710") } > db.demo217.insertOne({"FullName":" John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5d2b03d395bdc2134711") }
Display all documents from a collection with the help of find() method −
> db.demo217.find();
This will produce the following output −
{ "_id" : ObjectId("5e3e5d1e03d395bdc213470f"), "FullName" : " Chris Brown" } { "_id" : ObjectId("5e3e5d2503d395bdc2134710"), "FullName" : " David Miller" } { "_id" : ObjectId("5e3e5d2b03d395bdc2134711"), "FullName" : " John Doe" }
Following is the query to trim spaces from field in MongoDB −
> db.demo217.aggregate([ ... { $project: { Name: { $trim: { input: "$FullName" } } } } ... ])
This will produce the following output −
{ "_id" : ObjectId("5e3e5d1e03d395bdc213470f"), "Name" : "Chris Brown" } { "_id" : ObjectId("5e3e5d2503d395bdc2134710"), "Name" : "David Miller" } { "_id" : ObjectId("5e3e5d2b03d395bdc2134711"), "Name" : "John Doe" }
- Related Articles
- How to query on list field in MongoDB?
- How to limit the amount of characters returned from a field in a MongoDB query?
- MongoDB query by sub-field?
- MongoDB query for a single field
- MongoDB query to update array with another field?
- Trim a string in Java to remove leading and trailing spaces
- MySQL query to get a field value that does not contain empty spaces?
- How to add a field with static value to MongoDB find query?
- How to run MongoDB query to update only a specific field value?
- Indexing large text field to make query faster in MongoDB
- How to remove white spaces (leading and trailing) from string value in MongoDB?
- MongoDB query to change simple field into an object?
- MongoDB query select and display only a specific field from the document?
- Trim (Remove leading and trailing spaces) a string in C#
- How to query for records where field is null or not set in MongoDB?

Advertisements