- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to convert from ObjectId to String
To convert from ObjectId to String, use toString() in MongoDB. Let us create a collection with documents −
> db.demo52.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e27129bcfb11e5c34d89910") }
Display all documents from a collection with the help of find() method −
> db.demo52.find();
This will produce the following output −
{ "_id" : ObjectId("5e27129bcfb11e5c34d89910"), "StudentName" : "Chris" }
Following is the query to convert ObjectId to String −
> ObjectId("5e27129bcfb11e5c34d89910").toString(); ObjectId("5e27129bcfb11e5c34d89910")
Now you can check whether the ObjectId is in string or not −
> typeof ObjectId("5e27129bcfb11e5c34d89910").toString();
This will produce the following output −
String
Advertisements