
- 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
MongoDB query to change simple field into an object?
Fir this, you can use $rename. Let us first create a collection with documents −
> db.changeSimpleFieldDemo.insertOne({"StudentMarks":58,"StudentSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0847a825ddae1f53b62205") }
Following is the query to display all documents from a collection with the help of find() method −
> db.changeSimpleFieldDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5e0847a825ddae1f53b62205"), "StudentMarks" : 58, "StudentSubject" : "MySQL" }
Here is the query to change a field into an object. The “obj” field is a temporary field name we have used below −
> db.changeSimpleFieldDemo.update({}, {$rename: {Student: 'obj'}}, {multi: true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.changeSimpleFieldDemo.update({}, ... {$rename: {obj: 'Student.Marks', discountType: 'Student.Subject'}}, ... {multi: true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
- Related Articles
- MongoDB query to access an object in an array
- MongoDB query to push document into an array
- MongoDB query to match documents that contain an array field
- Query a nested field within an array with MongoDB
- MongoDB query by sub-field?
- MongoDB query to find data from an array inside an object?
- Filter documents in MongoDB using simple query?
- MongoDB query for Partial Object in an array
- MongoDB query to remove empty objects in an object-array?
- MongoDB query for a single field
- How to query on list field in MongoDB?
- MongoDB query to update array with another field?
- MongoDB query to set user defined variable into query?
- How to project grouping into object in MongoDB and display only the marks field?
- How to trim spaces from field in MongoDB query?

Advertisements