Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

objectid-basics

ObjectId basics

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
beginner

This lesson

This lesson teaches ObjectId basics: document modeling, query operators, and aggregation patterns for MongoDB.

Teams query ObjectId basics on every MongoDB codebase—skipping it leaves gaps in debugging and data reviews.

You will apply ObjectId basics in contexts like: Content catalogs, event logs, mobile sync backends, and polyglot stacks beside SQL services.

Copy JavaScript shell queries from each lesson into mongosh or MongoDB Atlas Data Explorer—inspect matched documents and explain plans. The in-browser lab (execution_profile: none) ships later; mongosh is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

If you omit _id on insert, MongoDB generates an ObjectId. Apps can use UUID strings or custom keys when needed.

Default _id

const r = db.items.insertOne({ name: 'Cable' })
r.insertedId
db.items.findOne({ _id: r.insertedId })

Practice: Run in mongosh on your practice database.

Custom _id

db.items.insertOne({ _id: 'SKU-1001', name: 'Adapter' })

Custom string IDs work when natural keys are stable—avoid hot shards on monotonic inserts at huge scale.

ObjectId in queries

const { ObjectId } = require('mongodb')
// In mongosh, insertedId is already ObjectId:
db.items.find({ _id: ObjectId('507f1f77bcf86cd799439011') })

Important interview questions and answers

  1. Q: ObjectId vs UUID?
    A: UUIDs are opaque strings; ObjectIds are BSON-native and compact.
  2. Q: Monotonic _id shard risk?
    A: Rising ObjectIds on one shard can create hotspots—hash or distribute keys.

Self-check

  1. What happens if insert omits _id?
  2. When might you choose a string _id?

Tip: Custom string _ids are fine for natural keys; avoid hot monotonic shard keys at huge scale.

Interview prep

Custom _id?
Allowed when natural keys are stable.
Hot shard risk?
Monotonic rising keys can concentrate writes on one shard.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • UUID vs ObjectId?
  • Hot shard key?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump