
- 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 convert value to string using $toString in MongoDB?
Let us see an example to understand the $toString in MongoDB. To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.objectidToStringDemo.insertOne({"UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c92b80036de59bd9de0639d") } > db.objectidToStringDemo.insertOne({"UserName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c92b80436de59bd9de0639e") } > db.objectidToStringDemo.insertOne({"UserName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c92b80936de59bd9de0639f") } > db.objectidToStringDemo.insertOne({"UserName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c92b81836de59bd9de063a0") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.objectidToStringDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c92b80036de59bd9de0639d"), "UserName" : "John" } { "_id" : ObjectId("5c92b80436de59bd9de0639e"), "UserName" : "Chris" } { "_id" : ObjectId("5c92b80936de59bd9de0639f"), "UserName" : "Larry" } { "_id" : ObjectId("5c92b81836de59bd9de063a0"), "UserName" : "Robert" }
Here is the query to convert ObjectId to a string value in MongoDB aggregate. The query is as follows −
> db.objectidToStringDemo.aggregate([ ... { ... $project: { ... _id: { ... $toString: "$_id" ... } ... } ... } ... ] ... );
The following is the output −
{ "_id" : "5c92b80036de59bd9de0639d" } { "_id" : "5c92b80436de59bd9de0639e" } { "_id" : "5c92b80936de59bd9de0639f" } { "_id" : "5c92b81836de59bd9de063a0" }
In order to get the original ObjectId, use the $toObjectId operator −
> db.objectidToStringDemo.aggregate([ { $project: { _id: { $toObjectId: "$_id" } } } ] );
The following is the output −
{ "_id" : ObjectId("5c92b80036de59bd9de0639d") } { "_id" : ObjectId("5c92b80436de59bd9de0639e") } { "_id" : ObjectId("5c92b80936de59bd9de0639f") } { "_id" : ObjectId("5c92b81836de59bd9de063a0") }
- Related Articles
- How to convert ObjectId to string in MongoDB
- Java Program to convert Double into String using toString() method of Double class
- How to convert string to numerical values in MongoDB?
- Convert Long into String using toString() method of Long class in java
- Convert NumberLong to String in MongoDB?
- Convert string to objectid in MongoDB?
- How to get the string representation of numbers using toString() in Java?
- How to convert a boolean value to string value in JavaScript?
- Java Program to convert string value to float using Float.valueOf()
- How to convert from string to date data type in MongoDB?
- MongoDB query to convert string to int?
- Convert Hexadecimal value String to ASCII value String in C++
- How to convert a Date value to string in JDBC?
- How to convert a double value to String in Java?
- MongoDB query to convert from ObjectId to String

Advertisements