Take the 2-minute tour ×

I'm new in mongodb and can't find the solution.

I'm using mongo-php-driver

I just created some collection. I want to create some document from PHP code.


When that code works, there are two same records in collection, with different _id.

{ "_id" : ObjectId("4ff4b3b8859183d41700000f"), "asda" : 12312, "cxzcxz" : "czczcxz" }
{ "_id" : ObjectId("4ff4b3b8859183d417000010"), "asda" : 12312, "cxzcxz" : "czczcxz" }

How to fix it, and what I need to change here to have only one document?

I have _id index in table. Maybe I need to set this key every time? When i setted _id field, it saved one record in collection. But how to make it auto (like auto increment) ?

share|improve this question
specify what library you are using to connect to mongoDB from php –  Ismael Abreu Jul 4 '12 at 21:38
that. github.com/mongodb/mongo-php-driver/downloads from official mongo docs –  Nikita Kuhta Jul 4 '12 at 21:40
shouldn't be insert instead of create? –  Ismael Abreu Jul 4 '12 at 22:39
insert not help. same problem. –  Nikita Kuhta Jul 5 '12 at 6:51
add comment

1 Answer

up vote 3 down vote accepted

You are able to insert more than one record with similar information because you have not specified a unique index on any of those values. The default unique index will be on _id.

You can define your own index from PHP using MongoCollection.ensureIndex eg:

// create a unique index on 'phonenum'
$collection->ensureIndex(array('phonenum' => 1), array("unique" => true));

It's also worth reading the MongoDB documentation on unique indexes as there are a few caveats to be aware of if a unique index is being created for an existing collection that may already have duplicates or null values.

You also have the option of providing your own _id value if there is a more natural primary key to use. You will have to ensure that this _id is unique for new insertions, though.

The default ObjectID created by MongoDB is designed to have a reasonably high probability of being unique when allocated.

Code example:


// Connect to MongoDB server
$mongo = new Mongo();

// Use database 'mydb' and collection 'mycoll'
$collection = $mongo->mydb->mycoll;

// Drop this collection for TESTING PURPOSES ONLY

// The document details to insert
$document = array(
    'asda' => 12312,
    'cxzcxz' => 'czczcxz',

try {
    $collection->insert($document, array("safe" => true));

    // Note that $collection->insert() adds the _id of the document inserted
    echo "Saved with _id:", $document['_id'], "\n";
catch (MongoCursorException $e) {

// Add unique index for field 'asda'
$collection->ensureIndex(array('asda' => 1), array("unique" => true));

// Try to insert the same document again
$document = array(
    'asda' => 12312,
    'cxzcxz' => 'czczcxz',
try {
catch (MongoCursorException $e) {

share|improve this answer
Can I do auto_increment field? Or something like that. To add some record to collection I will need to know new id, how to get it? –  Nikita Kuhta Jul 5 '12 at 5:46
MongoDB's ObjectID serves a similar purpose to an auto-increment field: provide an automatically generated unique identifier. Once your data starts being distributed across multiple servers via replica sets and sharding the concept of a centralized auto-increment number sequence is no longer relevant if you want writes to happen independently within your cluster. –  Stennie Jul 5 '12 at 6:42
@NikitaKuhta if you don't provide an _id when inserting a new document, a default ObjectID will be assigned. This is exactly what you are seeing in your example query in the original post .. you inserted two records and default ObjectIDs were added by the MongoDB server. –  Stennie Jul 5 '12 at 6:44
And it inserted TWO records, with different ObjectID. I want to add only one record, and I don't want to set any other unique index. Is it possible, or I need to set some another id field necessarily? –  Nikita Kuhta Jul 5 '12 at 6:47
I mean, that I use create (or insert) method only one time, and in collection inserts two records instead of one. I will watch unique logic by PHP scripts, but first I need to add only one record insead of two. –  Nikita Kuhta Jul 5 '12 at 6:50
show 4 more comments

Your Answer


