
- 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
Cast to ObjectId failed for value in MongoDB?
To cast to ObjectId correctly, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo460.insertOne({"_id":"5ab9cbfa31c2ab715d42129e"}); { "acknowledged" : true, "insertedId" : "5ab9cbfa31c2ab715d42129e" }
Display all documents from a collection with the help of find() method −
> db.demo460.find();
This will produce the following output −
{ "_id" : "5ab9cbfa31c2ab715d42129e" }
Following is the query to cast to objectId −
> db.demo460.aggregate( [ idConvert ])
This will produce the following output −
{ "_id" : "5ab9cbfa31c2ab715d42129e", "value" : ObjectId("5ab9cbfa31c2ab715d42129e") }
- Related Articles
- How to generate ObjectID in MongoDB?
- Convert string to objectid in MongoDB?
- How to convert ObjectId to string in MongoDB
- Display MongoDB records by ObjectId?
- MongoDB query to convert from ObjectId to String
- Find a document with ObjectID in MongoDB?
- MongoDB query to update a MongoDB row with only the objectid
- Create ObjectId in MongoDB using a seed string?
- Use ObjectId under findOne() to fetch a specific record in MongoDB?
- How to return documents of a collection without objectId in MongoDB?
- Is it possible to cast in a MongoDB Query?
- How we can perform sort on ObjectId column in MongoDB?
- MongoDB – Fix “Failed to convert from type String to type Date”?
- How to push value with for loop in MongoDB?
- Can we cast a double value to a byte in java?

Advertisements