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?
How to convert from string to date data type in MongoDB?
MongoDB query to convert string to int?
Java Program to convert string value to float using Float.valueOf()
Convert Hexadecimal value String to ASCII value String in C++
How to convert a double value to String in Java?
How to convert a Date value to string in JDBC?
MongoDB query to convert from ObjectId to String
Kickstart Your
Career
Get certified by completing the course
Get Started
Business Analytics Certification
Java & Spring Boot Advanced Certification
Data Science Advanced Certification
Cloud Computing And DevOps
Advanced Certification In Business Analytics
Artificial Intelligence And Machine Learning
DevOps Certification
Game Development Certification
Front-End Developer Certification
AWS Certification Training
Python Programming Certification
COMPILERS & EDITORS
Online Java Compiler
Online Python Compiler
Online Go Compiler
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online PHP Compiler
Online MATLAB Compiler
Online Bash Compiler
Online SQL Compiler
Online Html Editor
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects.