How to create ObjectId for document using Hexadecimal String ?

Problem Statement

Write mongoDB query for inserting document with ObjectId created from hexadecimal string.

Collection Name: students

Insert below document in collection student. And use hexadecimal 602e693f7e8df9001191c1a1 for objectid creation.

[
  {
    
    name: 'Joe Rogan',
    age: 35,
    country: 'USA'
  }
] 

To learn more about ObjectId and createFromHexString() : ObjectId.createFromHexString().

Solution

db.students.insertOne({
    _id:ObjectId.createFromHexString('602e693f7e8df9001191c1a1'),
    name: 'Joe Rogan',
    age: 35,
    country: 'USA',
})