2014년 12월 31일 수요일

"Error: invalid object id: length" when finding a document by _id

I started to learn MongoDB yesterday and I found a strange problem. I've inserted some dummy documents in a collection.

Then, I removed all of them using remove:
db.contas.remove({"_id": ObjectId("...."});

One of the documents can't be removed, it appears the error: "Error: invalid object id: length". P.S: All of the ids were generated randomly by MongoDB Java Driver.
If I try to find this document by _id it shows the same error (as you can see in image attached).

I already tried to repair the database, but it didn't work.

In this case, I'm just testing so I can remove the collection and start again. But, what I'd like to know what happened? Is it normal? How can I prevent it on production?





Looking at the attached image, it looks like a cut and paste error.  The hexadecimal value you included only has 23 characters, when it should have 24.  




Sorry... for my mistyping in the image attached.
I did another test (attached in the message), I did a find to list all documents, the document with _id: "54a33f2b07115c8169211aa1" is in that list, but when I try to find by this id it doesn't return anything.
With others ids it works.

I checked now, I hope there is no mistype ;)




Notice that the value of the _id field in that document is different than the other two: it's a string, not an ObjectId.  So when you search for it as an ObjectId you don't find it.  This is the correct behavior.



That's the point!
Thank you, I didn't notice this difference looking at the collection.


댓글 없음:

댓글 쓰기