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

4,160 views
Skip to first unread message

Rafael Viana

unread,
Dec 31, 2014, 7:28:02 AM12/31/14
to mongod...@googlegroups.com
Hi,

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?

Regards,
erro-mongo-db.png

Jeff Yemin

unread,
Dec 31, 2014, 11:25:25 AM12/31/14
to mongod...@googlegroups.com
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.  

Can you double check?


Regards,
Jeff

Rafael Viana

unread,
Dec 31, 2014, 3:33:41 PM12/31/14
to mongod...@googlegroups.com
Jeff,

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 ;)

Regards,
another-error.png

Jeff Yemin

unread,
Dec 31, 2014, 4:14:29 PM12/31/14
to mongod...@googlegroups.com
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.

Regards,
Jeff

Rafael Viana

unread,
Jan 1, 2015, 7:18:37 AM1/1/15
to mongod...@googlegroups.com
That's the point!
Thank you, I didn't notice this difference looking at the collection.

Regards,
Reply all
Reply to author
Forward
0 new messages