← Back to MongoDB Course | Chapter 2: CRUD — Create | Lesson 4 of 7

ObjectId क्या है

ObjectId default 12-byte unique _id है जो MongoDB हर document के लिए बनाता है।

In this page:

  1. ObjectId
Syntax
javascript
ObjectId()
ObjectId("hex_string")
document._id.getTimestamp()

ObjectId

एक ObjectId एक 4-byte timestamp, एक 5-byte random value, और एक 3-byte counter combine करता है, इसलिए ids machines के आर-पार unique हैं और creation time से roughly sortable हैं। getTimestamp() creation time extract करता है। _id field automatically indexed है।

Note: ObjectIds secrets नहीं हैं; इन्हें security tokens की तरह इस्तेमाल न करें।

उदाहरण: ObjectId

bash
test> const id = new ObjectId()
test> id
ObjectId('65a1b2c3d4e5f60718293a4d')
test> id.getTimestamp()
ISODate('2024-01-12T14:30:11.000Z')
test> typeof id.toString()
string
test> ObjectId.isValid("65a1b2c3d4e5f60718293a4d")
true

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ObjectIds को strings की तरह compare करना
  2. यह मान लेना कि ObjectIds sequential integers हैं
  3. इन्हें unguessable tokens की तरह इस्तेमाल करना
चैप्टर सारांश
  • ObjectId एक 12-byte unique id है
  • इसमें एक timestamp है
  • getTimestamp() time पढ़ता है
  • _id automatically indexed है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.