-- _id is always the primary key in MongoDB
-- but _id can contain any unique value
keyToMongoDoc :: (PersistEntity record, PersistEntityBackend record ~ DB.MongoContext)
                  => Key record -> DB.Document
keyToMongoDoc k = case entityPrimary $ entityDefFromKey k of
    Nothing   -> zipToDoc [DBName id_] values
    Just pdef -> [id_ DB.=: zipToDoc (primaryNames pdef)  values]
  where
    primaryNames = map fieldDB . compositeFields
    values = keyToValues k
keyToMongoDoc :: (PersistEntity record, PersistEntityBackend record ~ DB.MongoContext)
                  => Key record -> DB.Document
keyToMongoDoc k = case entityPrimary $ entityDefFromKey k of
    Nothing   -> zipToDoc [DBName id_] values
    Just pdef -> [id_ DB.=: zipToDoc (primaryNames pdef)  values]
  where
    primaryNames = map fieldDB . compositeFields
    values = keyToValues k
keyToMongoDoc k = case entityPrimary $ entityDefFromKey k of
    Nothing   -> zipToDoc [DBName id_] values
    Just pdef -> [id_ DB.=: zipToDoc (primaryNames pdef)  values]
  where
    primaryNames = map fieldDB . compositeFields
    values = keyToValues k