I'm guessing your code looks something like this (greatly simplified)?:
>>> doc = {}
>>> for i in xrange(2):
... doc['i'] = i
... c.shard_db.trackvars.insert(doc)
...
ObjectId('5166d479fa5bd835199a0638')
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "pymongo/collection.py", line 357, in insert
continue_on_error, self.__uuid_subtype), safe)
File "pymongo/mongo_client.py", line 916, in _send_message
rv = self.__check_response_to_last_error(response)
File "pymongo/mongo_client.py", line 857, in __check_response_to_last_error
raise DuplicateKeyError(details["err"])
pymongo.errors.DuplicateKeyError: E11000 duplicate key error index:
shard_db.trackvars.$_id_ dup key: { :
ObjectId('5166d479fa5bd835199a0638') }
The problem is that PyMongo injects an _id field into the document,
*if the _id field does not exist*, before inserting it (_id is always
generated client side with 10gen drivers). That means that the first
time through the loop _id is added by the insert method. Since "doc"
is defined outside the loop, each subsequent pass through the loop
uses the *same* value for _id.
If you want to modify some globally defined document in a loop this
way make sure you also chnage the _id value in the same loop. You can
create instances of bson.objectid.ObjectId in your code for this:
>>> from bson.objectid import ObjectId
>>> for i in xrange(2):
... doc['i'] = i
... doc['_id'] = ObjectId()
... c.shard_db.trackvars.insert(doc)
...
ObjectId('5166d4d5fa5bd835199a0639')
ObjectId('5166d4d5fa5bd835199a063a')
> --
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to
mongod...@googlegroups.com
> To unsubscribe from this group, send email to
>
mongodb-user...@googlegroups.com
> See also the IRC channel --
freenode.net#mongodb
>
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
mongodb-user...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>