0

I have a MongoDb collection named "users" with the following structure:

    {
        "_id" : ObjectId("akk34kt6"),
        "email" : "mymail@mail.com",
        "lastName" : "MyLastName",
        "firstName" : "MyFirstName",
        "password" : "password",
        "admin" : true
    }

I would like to change

"_id" : ObjectId("akk34kt6")

to

"_id" : "akk34kt6"

for every record. How can I do it from Mongo Shell ?

Cris69
  • 520
  • 1
  • 14
  • 40
  • I presume you are just abbreviating here as that would not be a valid value for an ObjectId. The real question here is why do you want to do this? But basically, just as with primary key values in the SQL world. You cannot "change" this. Only re-create. – Neil Lunn Apr 01 '14 at 10:48
  • You should build to query with objectids, storing the string representation of ObjectId would not be a good choice – Sammaye Apr 01 '14 at 10:59

1 Answers1

0

You can't update "_id". A possible answer to your question can be found at

How update the _id of one MongoDB Document?

Community
  • 1
  • 1
Saheed Hussain
  • 1,096
  • 11
  • 12
  • Searching a bit more I found the same issue here:(http://stackoverflow.com/questions/10929443/nodejs-mongodb-getting-data-from-collection-with-findone), my idea was to change the value of the _id record, instead I had to construct the ObjectID in the query. – Cris69 Apr 01 '14 at 14:11