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 6579265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:35+00:00 2026-05-25T15:52:35+00:00

Right now we’re doing all of our unique id generation by using MongoDB’s baked

  • 0

Right now we’re doing all of our unique id generation by using MongoDB’s baked in ids. We have some instances where we need to create an id for a object before we add anything to the database.

We could create an entry get the id and then delete the entry if we end up never needing it, but I would rather not put that load on the system for such an easy task. Is there any way to be in sync with mongos native methods for doing this without putting unnecessary load on the database?

Thanks

  • 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-05-25T15:52:36+00:00Added an answer on May 25, 2026 at 3:52 pm

    This is pretty simply to do. With the exception of upsert operations the ObjectId is client side generated. In other words, when you perform a save operation from within your app the driver will automatically do (pseudo):

    if(!doc.containsKey("_id"))
        doc.put(_id, new ObjectId());
    

    So all you have to do is generate an ObjectId yourself, do whatever you need to do with it and then set the _id of your document to that value rather than have the mongo driver do it for you.

    Note that this is exactly as safe as having the driver generated the IDs. ObjectIds are designed to eliminate ID collisions (read: make it unlikely to the point of collisions becoming irrelevant). Also, the time between generating the id and storing it also does not introduce ID collision issues.

    If you save a document with an _id that already exists in the database it will overwrite the original document with that _id.

    If you insert a document with an _id that already exists you will get a duplicate _id error (error 11000).

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

Sidebar

Related Questions

Right now, I have three models Post, Comment and User (using Devise ) associated
Right now I have some TabItems in my App which are implicitly styled. I
Right now I have the following query: User.find(:all, :conditions => [guest = ? AND
Right now I'm using the jQuery selector $('foo+bar') . Since I already have $('foo')
Right now, I have configuration setting for morphia talks to one MongoDB from Play!.
Right now we have a dll file that contains all the database calls and
Right now, I'm using this to allow a file to be moved to our
Right now I am using std::pair to represent a 2d point in c++. However,
Right now I have this SQL query which is valid but always times out:
Right now I have def min(array,starting,ending) minimum = starting for i in starting+1 ..ending

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.