mongodbstudio3t

Update Object ID of collection


I have my own collection in mongo db but by default it generated a unique object id ? How can i replace it with my own object id in studio 3T or compass?

I tried manually inserting the object id with JSON doesn't seem to be working


Solution

  • Instead of editing the default ID generated by mongo I suggest you to use a different attribute name as a unique id and leave _id as it is. You can create a unique index in your collection with this command db.collection.createIndex( { "my_id": 1 }, { unique: true } )

    You cannot edit the immutable field autogenerated "_id" but you can set it when inserting the object to the collection (i.e. db.collection.insert({"_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa")})).