0

I have imported my database entries from a csv file into mongodb and in the process of doing so the imported entries's '_id's aren't in the typical randomised format. e.g instead of looking like

"_id": "R6AnePqKecNNe7dkr"

they look like

"_id": 28

For some reason this means that when I try to refer to the database entry with an _id of 28 it can't find it. I thought it might be an issue in that they _id is not a string because they're not in quotes but I've tried to convert them to strings and that doesn't make a difference. I've tried making a function that randomly generates a new _id but it can't find the old _id to put the new _id into.

Is there any way to do a bulk "generate new _ids for everything in this collection"? I'm having trouble finding anything equivalent

Josh McGee
  • 443
  • 6
  • 16
  • 1
    There's nothing wrong with numerical `_id` values as long as they're all unique. `db.test.findOne({_id: 28})` would find it. – JohnnyHK Jun 20 '16 at 02:04
  • thanks for your help! my problem was that I was getting the id as a parameter from the URL which was a string and since the _id wasn't a string I had to convert it into a number before using it in findOne – Josh McGee Jun 20 '16 at 21:33
  • Did you fix that or still in trouble? – profesor79 Jun 21 '16 at 09:29
  • @profesor79 thanks, all fixed - the problem ended up being unrelated to how I phrased the question – Josh McGee Jun 21 '16 at 23:35
  • FYI: the `_id` field is immutable, so to change to a new value you need to copy affected documents and re-insert with a new (or empty) `_id` similar to [How update the _id of one MongoDB Document?](http://stackoverflow.com/a/4012997). – Stennie Jun 22 '16 at 02:21

0 Answers0