
- 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 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
- Related Articles
- Convert string to objectid in MongoDB?
- How to convert ObjectId to string in MongoDB
- MongoDB query to convert string to int?
- MongoDB query to convert numeric string to number
- MongoDB query to update a MongoDB row with only the objectid
- MongoDB query to convert a string with commas to double
- How to generate ObjectID in MongoDB?
- Create ObjectId in MongoDB using a seed string?
- How to convert from string to date data type in MongoDB?
- MongoDB – Fix “Failed to convert from type String to type Date”?
- Cast to ObjectId failed for value in MongoDB?
- Convert NumberLong to String in MongoDB?
- Display MongoDB records by ObjectId?
- MongoDB query to get array of nested string?
- MongoDB query to remove item from array?

Advertisements