'When fields are specified with [~fieldname] syntax, where
*fieldname* is the Python name of a field, *fieldname* will be
substituted for the MongoDB name of the field (specified using the
:attr:`name` keyword argument in a field\'s constructor).'

def _sub_js_fields(self, code):

    def field_sub(match):
        field_name = match.group(1).split('.')
        fields = self._document._lookup_field(field_name)
        return (u'["%s"]' % fields[(-1)].db_field)
    def field_path_sub(match):
        field_name = match.group(1).split('.')
        fields = self._document._lookup_field(field_name)
        return '.'.join([f.db_field for f in fields])
    code = re.sub(u'\\[\\s*~([A-z_][A-z_0-9.]+?)\\s*\\]', field_sub, code)
    code = re.sub(u'\\{\\{\\s*~([A-z_][A-z_0-9.]+?)\\s*\\}\\}', field_path_sub, code)
    return code
