Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
Does mongodb automatically create an index on the _id field of embedded documents?

Using mongodb and mongoose on a node.js project and I was wondering do I have to ensureIndex on the _id fields of embedded items?

That is, I see that mongodb automatically creates an index on _id fields of collections, does it also do the same for _id fields of embedded collections?

{
   _id: "joe",
   name: "Joe Bookreader",
   addresses: [
                {
                  _id: "someid1",
                  street: "123 Fake Street",
                  city: "Faketon",
                  state: "MA",
                  zip: "12345"
                },
                {
                  _id: "someid2",
                  street: "1 Some Other Street",
                  city: "Boston",
                  state: "MA",
                  zip: "12345"
                }
              ]
 }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

My instinct was no, so I just tried it:

> db.foo.find()

> db.foo.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "foo.foo"
    }
]

> db.foo.insert({
...    _id: "joe",
...    name: "Joe Bookreader",
...    addresses: [
...                 {
...                   _id: "someid1",
...                   street: "123 Fake Street",
...                   city: "Faketon",
...                   state: "MA",
...                   zip: "12345"
...                 },
...                 {
...                   _id: "someid2",
...                   street: "1 Some Other Street",
...                   city: "Boston",
...                   state: "MA",
...                   zip: "12345"
...                 }
...               ]
...  })
WriteResult({ "nInserted" : 1 })

> db.foo.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "foo.foo"
    }
]
>

Looks like no it does not automatically create indexes on subdocuments.

over 4 years ago · Santiago Trujillo Report

0

From MongoDB documentation:

By default, MongoDB creates a unique index on the _id field during the creation of a collection.
over 4 years ago · Santiago Trujillo Report

0

Yes its does according to documentation mongo dB creates a unique index on _id field. Default _id Index

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!