code: def insert_with_id(obj):
      
      if '_id' in obj:
          raise ValueError("object already has '_id' field")
  
      # add created_at/updated_at on insert
      obj['created_at'] = datetime.datetime.utcnow()
      obj['updated_at'] = obj['created_at']
  
      if obj['_type'] == 'person' or obj['_type'] == 'legislator':
          collection = db.legislators
          id_type = 'L'
      elif obj['_type'] == 'committee':
          collection = db.committees
          id_type = 'C'
      elif obj['_type'] == 'bill':
          collection = db.bills
          id_type = 'B'
      else:
          raise ValueError("unknown _type for object")
  
      # get abbr
      abbr = obj[settings.LEVEL_FIELD].upper()
  
      id_reg = re.compile('^%s%s' % (abbr, id_type))
  
      # Find the next available _id and insert
      id_prefix = '%s%s' % (abbr, id_type)
      cursor = collection.find({'_id': id_reg}).sort('_id', -1).limit(1)
  
      try:
          new_id = int(next(cursor)['_id'][len(abbr) + 1:]) + 1
      except StopIteration:
          new_id = 1
  
      while True:
          if obj['_type'] == 'bill':
              obj['_id'] = '%s%08d' % (id_prefix, new_id)
          else:
              obj['_id'] = '%s%06d' % (id_prefix, new_id)
          obj['_all_ids'] = [obj['_id']]
  
          if obj['_type'] in ['person', 'legislator']:
              obj['leg_id'] = obj['_id']
  
          try:
              return collection.insert(obj, safe=True)
          except pymongo.errors.DuplicateKeyError:
              new_id += 1
docs: Generates a unique ID for the supplied legislator/committee/bill
      and inserts it into the appropriate collection.