Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:29:25+00:00 2026-06-03T20:29:25+00:00

I have a webapp where I’m created an embedded document for educations. The job

  • 0

I have a webapp where I’m created an embedded document for educations. The job document looks something like this:

"educations" : [
    {
        "school" : "Brandywine High School",
        "major" : "Testingasdf",
        "grad_year" : ISODate("1979-01-01T00:00:00Z"),
        "school_type" : "Graduate",
        "_id" : ObjectId("4fb26c9ce5be08208b000ce4")
    }
],
"email" : "user@domain.com",

The education hash has the details of the job. I noticed that if i create the hash without an ID:

User.collection.update(
  { _id: @user.id },
  { :$push => { educations: education } },
  { safe: true }
)

and I query the education from the Rails console, the ID will change each time:

irb(main):004:0> User.brandon.educations.map(&:id)
 => [BSON::ObjectId('4fb26e13e5be082384000007')]
irb(main):005:0> User.brandon.educations.map(&:id)
 => [BSON::ObjectId('4fb26e13e5be082384000009')]

However, if I do this:

User.collection.update(
  { _id: @user.id },
  { :$push => { educations: BSON::ObjectId.create_pk(education) } },
  { safe: true }
)

The ID is the same each time queried from the console. Because of this, I’m having trouble referencing the education to edit the embedded doc in the browser.

Do I always have to supply a BSON ID when I create an embedded document?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:29:27+00:00Added an answer on June 3, 2026 at 8:29 pm

    If you don’t supply some kind of identifier (it could be a String, an int, etc) in the _id field, then MongoDB will automatically create one for you, to make the document unique (so that it can be looked up by _id). There is normally a unique index on _id, so if the document isn’t unique, insertion will fail (or the existing document will be updated instead, etc).

    MongoDB’s strategy for making the document unique is to use an ObjectID, as these are globally unique. They are also different every time you create one — that’s how they are globally unique.

    Long story short: if you have a key that makes your document 100% unique already (possibly :school in your example), then store it as _id, and MongoDB will do the hard work for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have webapp in JSF2, something like mywebdomain.com/mywebsite/portal/index.xhtml Now I need to add an
OKay I lost alsmost the whole day on this. I have a webapp where
I have created a webapp using maven2 archetype in netbeans 6.7. when I do
I have this webnoir webapp as an Eclipse project. You can download it here:
I have webapp which has a jdbc driver dependency. And this is dependency's scope
I have a webapp written in PHP using a MySQL database backend. This question
I have this webapp for ios and in a certain page i have an
I have a webapp from which I'd like to insert diagrams and images quickly
We have this webapp product being deployed for multiple customers and think about switching
I have a webapp, where people signup and get a sub domain under this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.