这是indexloc提供的服务,不要输入任何密码
Skip to content

escape names with invalid characters in mongodb query field selection #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

hallettj
Copy link
Collaborator

We had a bug when selecting fields with names containing dollar signs or dots, or when selecting fields using aliases containing those characters. What happened is the mongodb query plan would produce a stage like this:

{
  "$replaceWith": {
    "foo.bar": "$title"
  }
}

The use of "foo.bar" leads to this error:

MongoServerError: FieldPath field names may not contain '.'. Consider using $getField or $setField.

The fix is, as the error suggests, to use the $setField operator which takes a field argument that is not evaluated as an expression, and is therefore allowed to contain arbitrary characters.

{
  "$replaceWith": {
    "$setField": {
      "field": "foo.bar",
      "value": "$title",
      "input": "$$CURRENT"
    }
  }
}

But that leads to an issue when we want to select multiple fields: in the first snippet the argument to $replaceWith was an object literal where we could set multiple fields. OTOH { "$setField": { ... } } is an expression that produces an object with a single field, and does not accept more keys. To select multiple fields with escaping we have to nest calls to $setField for every selected field. For example,

{
  "$replaceWith": {
    "$setField": {
      "field": "foo.bar",
      "value": "$title",
      "input": {
        "$setField": {
          "field": "year",
          "value": "$year",
          "input": {
            "$setField": {
              "field": "rated",
              "value": "$rated",
              "input": "$$CURRENT"
            }
          }
        }
      }
    }
  }
}

So that is what this PR does.

@hallettj hallettj self-assigned this Jul 23, 2025
@hallettj hallettj merged commit fc3018a into v1 Jul 23, 2025
2 checks passed
@hallettj hallettj deleted the jessehallett/eng-1840-mongodb-connector-fails-on-aggregate-selection-due-to branch July 23, 2025 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants