

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to generate ObjectID in MongoDB?
- Convert string to objectid in MongoDB?
- Display MongoDB records by ObjectId?
- How to convert ObjectId to string in MongoDB
- 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?
- Sorting field value (FirstName) for MongoDB?
- Fix for java.math.BigInteger cannot be cast to java.lang.Integer?
- How to push value with for loop in MongoDB?
Advertisements