Populating a collection using a custom field instead of _id

You simply set _id to any value. The only restriction is that it has to be unique within a collection. Some use UUID rather than generated ObjectId.

For example, in mongosh:

tlas atlas-d7b9wu-shard-0 [primary] test> c.insertOne( { _id : 0 } )
{ acknowledged: true, insertedId: 0 }
Atlas atlas-d7b9wu-shard-0 [primary] test> c.insertOne( { _id : new Date( "2023-01-31" ) } )
{ acknowledged: true, insertedId: ISODate("2023-01-31T00:00:00.000Z") }
Atlas atlas-d7b9wu-shard-0 [primary] test> c.insertOne( { _id : 20230131 } )
{ acknowledged: true, insertedId: 20230131 }
Atlas atlas-d7b9wu-shard-0 [primary] test> c.insertOne( { _id : new UUID() } )
{
  acknowledged: true,
  insertedId: UUID("ab2e3ede-d8b0-46ba-9047-5bfffdd475fb")
}
Atlas atlas-d7b9wu-shard-0 [primary] test> c.insertOne( { _id : { year : 2023 , month : 1 , day : 31 } } )
{ acknowledged: true, insertedId: { year: 2023, month: 1, day: 31 } }
Atlas atlas-d7b9wu-shard-0 [primary] test> c.find()
[
  { _id: 0 },
  { _id: ISODate("2023-01-31T00:00:00.000Z") },
  { _id: 20230131 },
  { _id: UUID("ab2e3ede-d8b0-46ba-9047-5bfffdd475fb") } ,
  { _id: { year: 2023, month: 1, day: 31 } }
]

As you see, you may even mix different types. It’s unusual but possible.

Sometimes, it may be more efficient to use a different _id. For example, US ZIP codes are unique, so you could use the ZIP code it self as the _id, rather than having a normal ObjectId with its index and the another field zipcode with another index. More efficient because you spare 1 index.

2 Likes