Reputation: 1403
(I'm new to mongo)
How do I query for an _id that's not of type ObjectId?
I mongorestore'd a mongodump and can't get any results based on the id field. I noticed after a while that the records in the collection didn't adhere to the ObjectID type.
"_id": "7409a2ab-322e-40f3-b991-c3ebeda78f61",
If I change the id to id:"5c47422231caf0939f3d7cc7"
then my query will work correctly since that matches the ObjectID type.
I just keep getting CastError: Cast to ObjectId failed for value "f7757753-c450-42f9-b0f9-9a617628fcfa" at path "_id" for model "mydb"
I've tried querying mydb.findById({ _id: 'f7757753-c450-42f9-b0f9-9a617628fcfa' })
and mydb.findOne({ _id: 'f7757753-c450-42f9-b0f9-9a617628fcfa' }
and mydb.findOne({ _id: 'f7757753-c450-42f9-b0f9-9a617628fcfa' }
also tried incorporating ObjectId casting into my queries like so: { _id: ObjectId("f7757753-c450-42f9-b0f9-9a617628fcfa") }
Upvotes: 4
Views: 1515
Reputation: 1155
in your model you say you have Object id as the data type of ID right? So it's normal to scream the cast error when you use this
7409a2ab-322e-40f3-b991-c3ebeda78f61
as the _id.. So you need to change your model( schema actually) in a way that your type if the id will be a string not a object id
{
_id : String,
blah,
blah,
blah,
}
Upvotes: 3