
- 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
Convert a field to an array using update operation in MongoDB
To convert a field to an array, use UPDATE operation inside forEach(). Let us first create a collection with documents −
> db.demo18.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1387fc55d0fc6657d21f0e") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo18.find();
This will produce the following output −
{ "_id" : ObjectId("5e1387fc55d0fc6657d21f0e"), "StudentName" : "John" }
Here is the query to convert a field to an array using update operation −
> db.demo18.find().forEach(function(myDocument) { ... db.demo18.update( ... { _id: myDocument._id }, ... { "$set": { "StudentName": [myDocument.StudentName] } } ... ); ... })
Following is the query to display all documents from a collection with the help of find() method −
> db.demo18.find();
This will produce the following output −
{ "_id" : ObjectId("5e1387fc55d0fc6657d21f0e"), "StudentName" : [ "John" ] }
- Related Articles
- Convert a field to an array using MongoDB update operation?
- MongoDB query to update an array using FindAndUpdate()?
- MongoDB query to update array with another field?
- Update field in exact element array in MongoDB?
- Update MongoDB field using value of another field?
- Update an array element matching a condition using $push in MongoDB
- Want to update inner field in a MongoDB
- MongoDB query to update an array element matching a condition using $push?
- Update _id field in MongoDB
- How to update _id field in MongoDB?
- Update elements inside an array in MongoDB?
- Add a field to an embedded document in an array in MongoDB?
- Update multiple elements in an array in MongoDB?
- Removing an array element from MongoDB collection using update() and $pull
- How to update a single field in a capped collection in MongoDB?

Advertisements