
- 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 update MongoDB collection using $toLower?
There is a $toLower operator in MongoDB to be used as part of aggregate framework. But, we can also use the for loop to iterate over the specific field and update one by one.
Let us first create a collection with documents
> db.toLowerDemo.insertOne({"StudentId":101,"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4515e86fd1496b38bf") } > db.toLowerDemo.insertOne({"StudentId":102,"StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4b15e86fd1496b38c0") } > db.toLowerDemo.insertOne({"StudentId":103,"StudentName":"CHris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5115e86fd1496b38c1") } > db.toLowerDemo.insertOne({"StudentId":104,"StudentName":"ROBERT"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5a15e86fd1496b38c2") }
Following is the query to display all documents from a collection with the help of find() method
> db.toLowerDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b1b4515e86fd1496b38bf"), "StudentId" : 101, "StudentName" : "John" } { "_id" : ObjectId("5c9b1b4b15e86fd1496b38c0"), "StudentId" : 102, "StudentName" : "Larry" } { "_id" : ObjectId("5c9b1b5115e86fd1496b38c1"), "StudentId" : 103, "StudentName" : "CHris" } { "_id" : ObjectId("5c9b1b5a15e86fd1496b38c2"), "StudentId" : 104, "StudentName" : "ROBERT" }
Following is the query to update MongoDB like $toLower
> db.toLowerDemo.find().forEach( ... function(lower) { ... lower.StudentName = lower.StudentName.toLowerCase(); ... db.toLowerDemo.save(lower); ... } ... );
Let us check the document once again from the above collection. Following is the query
> db.toLowerDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b1b4515e86fd1496b38bf"), "StudentId" : 101, "StudentName" : "john" } { "_id" : ObjectId("5c9b1b4b15e86fd1496b38c0"), "StudentId" : 102, "StudentName" : "larry" } { "_id" : ObjectId("5c9b1b5115e86fd1496b38c1"), "StudentId" : 103, "StudentName" : "chris" } { "_id" : ObjectId("5c9b1b5a15e86fd1496b38c2"), "StudentId" : 104, "StudentName" : "robert" }
- Related Articles
- How to update an existing document in MongoDB collection using Java?
- How to Update multiple documents in a MongoDB collection using Java?
- MongoDB concurrent update with sub collection?
- Removing an array element from MongoDB collection using update() and $pull
- How to update a single field in a capped collection in MongoDB?
- MongoDB query to update a specific document from a collection
- How to update or modify the existing documents of a collection in MongoDB?
- How to create a MongoDB collection using Java?
- How to drop a MongoDB Collection using Java?
- Using findOneAndUpdate () to update in MongoDB?
- How to update MongoDB Object?
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to update an array using FindAndUpdate()?
- How to sort the documents of a MongoDB collection using java?
- How to insert a document into a MongoDB collection using Java?

Advertisements