
- 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
Make MongoDB replace single array value with string?
To replace, use $set and positional($) operator. Let us create a collection with documents −
> db.demo564.insertOne({"StudentName":["Chris","David","Mike","Sam"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e90880a39cfeaaf0b97b576") }
Display all documents from a collection with the help of find() method −
> db.demo564.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e90880a39cfeaaf0b97b576"), "StudentName" : [ "Chris", "David", "Mike", "Sam" ] }
Following is the query to replace single array value with string −
> db.demo564.updateMany( ... { "StudentName": "David" }, ... { "$set": { "StudentName.$": "Carol Taylor" } } ... ) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } >
Display all documents from a collection with the help of find() method −
> db.demo564.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e90880a39cfeaaf0b97b576"), "StudentName" : [ "Chris", "Carol Taylor", "Mike", "Sam" ] }
- Related Articles
- Replace an array field value with MongoDB?
- Replace value with a string literal during MongoDB aggregation operation
- MongoDB query to replace value in an array?
- MongoDB query to replace value with aggregation?
- Script to add a single value to array in MongoDB collection?
- Query array of nested string with MongoDB?
- Decrement only a single value in MongoDB?
- Increment value of an array element with array object in MongoDB
- Coalesce values from different properties into a single array with MongoDB aggregation
- MongoDB aggregate to convert multiple documents into single document with an array?
- Find value in a MongoDB Array with multiple criteria?
- How to pop a single value in MongoDB?
- Increment only a single value in MongoDB document?
- JavaScript - convert array with null value to string
- How to use JavaScript to replace a portion of string with another value?

Advertisements