# How to generate random MongoDB ID in postman

**URL:** <https://community.postman.com/t/how-to-generate-random-mongodb-id-in-postman/76537>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [March 6, 2025, 4:05pm UTC](https://community.postman.com/t/how-to-generate-random-mongodb-id-in-postman/76537 "2025-03-06T16:05:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasir-barati](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kasir-barati/32/31858_2.png) [@kasir-barati](https://community.postman.com/u/kasir-barati)\
**Post date:** [March 6, 2025, 4:05pm UTC](https://community.postman.com/t/how-to-generate-random-mongodb-id-in-postman/76537/1 "2025-03-06T16:05:57Z")

</div>

I know we have `{{$GUID}}` to generate a random UUID v4, but do we have something similar for Object ID in postman?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 6, 2025, 4:39pm UTC](https://community.postman.com/t/how-to-generate-random-mongodb-id-in-postman/76537/2 "2025-03-06T16:39:05Z")

</div>

Hey @kasir-barati 👋

Welcome to the Postman Community! :postman:

Do you have an example of what that is?

You can use the scripting sandbox to create a random value that matches that using javascript and store that as a variable. That could be used in the request as a MongoDB Id.

I don’t actually know what the format of that id is though.

* * *

Not sure how useful that would be but this is what Postbot created for me:

```auto
function createObjectId() {
    const timestamp = Math.floor(Date.now() / 1000).toString(16); // 4 bytes (8 hex chars)
    const machineId = Math.random().toString(16).slice(2, 8).padEnd(6, '0'); // 3 bytes (6 hex chars)
    const processId = (Math.floor(Math.random() * 65536)).toString(16).padStart(4, '0'); // 2 bytes (4 hex chars)
    const counter = Math.floor(Math.random() * 16777216).toString(16).padStart(6, '0'); // 3 bytes (6 hex chars)

    return timestamp + machineId + processId + counter;
}

console.log(createObjectId()); // Example output: "65e93bff8a4d5c2b3e6f8bcd"

```
