{"record":{"id":"3c7e5fe015212dc1","repo":"mongodb/mongoid","slug":"eager-load-cannot-use-lookup-aggregation-because","errorCode":null,"errorMessage":"eager_load cannot use $lookup aggregation because the following associations reside in a different cluster or database than #{klass} (client: #{klass.client_name}, database: #{klass.database_name}): #{offender_descriptions.join(', ')}. Falling back to #includes behavior.","messagePattern":"eager_load cannot use \\$lookup aggregation because the following associations reside in a different cluster or database than #(.+?) \\(client: #(.+?), database: #(.+?)\\): #(.+?)\\. Falling back to #includes behavior\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/mongoid/association/eager_loadable.rb","lineNumber":44,"sourceCode":"        docs.tap do |d|\n          preload(criteria.inclusions, d) if eager_loadable?\n        end\n      end\n\n      # Load the associations for the given documents using $lookup.\n      #\n      # If any of the associated collections reside in a different cluster or\n      # database than the root class, falls back to the #includes behavior and\n      # logs a warning.\n      #\n      # @return [ Array<Mongoid::Document> ] The given documents.\n      def eager_load_with_lookup\n        offenders = inclusions_unreachable_by_lookup\n        if offenders.any?\n          offender_descriptions = offenders.map do |offender|\n            \"#{offender.name} (client: #{offender.klass.client_name}, database: #{offender.klass.database_name})\"\n          end\n          Mongoid.logger.warn(\n            'eager_load cannot use $lookup aggregation because the following associations ' \\\n            \"reside in a different cluster or database than #{klass} \" \\\n            \"(client: #{klass.client_name}, database: #{klass.database_name}): \" \\\n            \"#{offender_descriptions.join(', ')}. Falling back to #includes behavior.\"\n          )\n          return eager_load(docs_for_lookup_fallback)\n        end\n\n        through_inclusions = criteria.inclusions.select do |association|\n          association.is_a?(Association::Referenced::HasOneThrough) ||\n            association.is_a?(Association::Referenced::HasManyThrough)\n        end\n\n        if through_inclusions.any?\n          through_names = through_inclusions.map { |association| \":#{association.name}\" }\n          Mongoid.logger.warn(\n            \"#{through_names.join(', ')} are :through associations and do not support \" \\\n            '$lookup-based eager loading. All inclusions for this query will be preloaded ' \\","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/association/eager_loadable.rb#L26-L62","documentation":"Error \"eager_load cannot use $lookup aggregation because the following associations reside in a different cluster or database than #{klass} (client: #{klass.client_name}, database: #{klass.database_name}): #{offender_descriptions.join(', ')}. Falling back to #includes behavior.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/association/eager_loadable.rb:44 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}