title: MongoDB “E11000 duplicate key error index” when creating saving a mongoengine ReferenceField (DBRef)
body: <p>I am attempting to load testing fixtures from a json file in my test runs (to match a similar style of fixture loading used in Django). This is the code I've got so far;</p>
  
  <pre><code>from django.utils import simplejson as json
  from mongoengine import base as mongobase
  from mongoengine import connect
  from pymongo import json_util
  
  db = connect("DATABASENAME")
  
  # Clear out the current collections.
  for collection in db.collection_names():
      if collection == 'system.indexes':
          continue
      db.drop_collection(collection)
  
  
  # Open json fixtures
  
  f = open('path/to/test_fixtures.json', 'r')
  datas = json.loads(f.read(), object_hook=json_util.object_hook)
  
  # For each serialised model instance, loop through and save to the database.
  
  for data in datas:
  
      print data
  
      if data['_cls'] not in mongobase._document_registry:
          print "Skipping %s" % data['_cls2']
          continue
  
      model = mongobase._document_registry[data['_cls']]
      model_instance = model._from_son(data)
      model_instance.save(force_insert=True)
  </code></pre>
  
  <p>This mostly seems to work, however, when one of the models has a ReferenceField, it fails. Complaining about a duplicate key. You'll notice there is a print statement in there to show the dict contents, in an example run I'll get the following output which all looks fine before the error</p>
  
  <pre><code>{u'_types': [u'Account'], u'status': u'ok', u'name': u'Alice', u'local_id': u'3', u'_cls': u'Account', u'members': [], u'_id': ObjectId('4f17f0855585d32457000001'), u'email': u'alice@example.com', u'permissions': []}
  {u'_types': [u'Account'], u'status': u'ok', u'name': u'Bob', u'local_id': u'2', u'_cls': u'Account', u'members': [], u'_id': ObjectId('4f17f0855585d32457000000'), u'email': u'bob@example.com', u'permissions': []}
  {u'_types': [u'Account'], u'status': u'ok', u'name': u'company', u'_cls': u'Account', u'members': [], u'_id': ObjectId('4f17f0855585d32457000002'), u'email': u'org@example.com', u'permissions': []}
  {u'_types': [u'Membership'], u'parent_account': DBRef(u'account', ObjectId('4f17f0855585d32457000002')), u'member': DBRef(u'account', ObjectId('4f17f0855585d32457000001')), u'role': u'member', u'_cls': u'Membership', u'_id': ObjectId('4f17f0855585d32457000003')}
  </code></pre>
  
  <p>Finally, the error is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/django/test/testcases.py", line 292, in __call__
      self._pre_setup()
    File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/django/test/testcases.py", line 259, in _pre_setup
      self._fixture_setup()
    File "/vagrant/engineclub/engineclub/apps/notifications/tests.py", line 67, in _fixture_setup
      model_instance.save(force_insert=True)
    File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/mongoengine/document.py", line 177, in save
      _refs=_refs)
    File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/mongoengine/document.py", line 183, in save
      raise OperationError(message % unicode(err))
  OperationError: Tried to save duplicate unique keys (E11000 duplicate key error index: test_aliss.account.$_id_  dup key: { : ObjectId('4f17f0855585d32457000001') })
  </code></pre>
  
  <p>It seems to be complaining about a duplicate key on account.$<em>id</em> when inserting a <em>membership</em> instance which has a reference to the account collection only.</p>
  
  <p>Let me know if there is any other information I can include, I would have added the JSON file however, its very much like the printed dicts (and only has 3 accounts followed by one membership). One thing I did notice, when I remove the force_insert from the save, it doesn't seem to save anything at all (and thus appears to pass fine).</p>