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

  • Home
  • SEARCH
  • 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 6195761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:31:28+00:00 2026-05-24T03:31:28+00:00

I am trying to setup mongodb to test out its speed and am running

  • 0

I am trying to setup mongodb to test out its speed and am running into a issue with _id duplication. I am not setting the is, I am letting mongodb do as I don’t care. I ahve the following php code:

<?php
$mongo = new Mongo();
$db = $mongo->selectDB("scrap_fighters");
$collection = $db->selectCollection('scores');

$data = array
(
  'user_id' => 1,
  'name' => 'John Doe',
  'score' => 120
);

$start = microtime(true);
for($x=0; $x < 1000; $x++)
{
  $data['unqiue'] = microtime();
  $result = $collection->insert($data, array('safe' => true));
}
?>

What does mongodb use to generate thier “unique” ids? I even tried replacing unique with:

$data['unqiue'] = rand(1, 1000000);

To be 100% sure it was working but it still failed after the first write. can I not enter records with the same data without specific generation a unique id myself?

  • 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-24T03:31:28+00:00Added an answer on May 24, 2026 at 3:31 am

    MongoDB uses _id as primary key, and as such it has to be unique. Since you don’t specify it, it will automatically generated. It consists of a microsecond timestamp and a hash based on the host, so even there are multiple hosts inserting simultaneously the probabily of collision is extremely low.

    What is this unique field you are using? If you wanted it to be a primary key just don’t set it.

    About failing on duplicates: the only reason I can think of this happening is that you previously set up an index on this collection which requires some field (or combination of fields) to be unique. If not needed, remove it. If it’s valid (eg: the user_id has to be unique) then insert unique records.

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

Sidebar

Related Questions

In trying to setup a unit test for inserting an item into an SQL
While trying to setup to use the Geospatial Index on MongoDB, I run into
I'm trying to setup mocks for mongodb in my java code, and I'm getting
I'm trying to set up mongodb on webfaction. It works fine, currently running on
I m trying setup a cassandra cluster as a test bed but gave the
im trying to setup sqlite as a secondary adapter and have run into a
Trying to setup a test page that access a video hosted on S3 and
Im trying to setup a blog with tagging, and i've run into a problem
Trying to setup a CodeIgniter based project for local development (LAMP stack), and once
Trying to setup some validation on the add to cart button for the dropdown

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.