Skip to content

$.query is broken in join / having on mongo driver #126

@ForkKILLET

Description

@ForkKILLET

Code:

const words = await this.ctx.database
  .join(
    { message: 'w-message', word: 'w-message-word' },
    ({ message, word }) => $.and(
      $.eq(message.id, word.messageId),
      $.query(message, {
        ...guildQuery,
        ...userQuery,
        ...durationQuery,
      })
    )
  )
  .project(['word.word'])
  .execute()

Error:

MongoServerError: An object representing an expression must have exactly one field: { $expr: true, platform: "onebot", guildId: "*********", timestamp: {} }

Cause:

  • $.query evaluates to { $expr, ...query }

  • the join query is assigned to sel.args[0].having and handled by (Builder#modifier -> Builder#eval ->)Builder#transformEvalExpr

  • $expr falls through, causing the Mongo error

    • however, $expr ($.quote) in where queries is handled correctly, by Builder#query)

Possible fix (untested):

if ('$expr' in query) {
  const { $expr, ...restQuery } = query
  return { $and: [{ $expr }, this.transformEvalExpr(restQuery, group)] }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions