Mongo readPreference ignored?

Hi,

I have a cluster with 3 nodes. (1x primary, 2x secondary). My application DSN contains readPreference=secondary, however I see many READ slowlogs comming from the primary node. Any idea, why?

{
  "timestamp": "2023-11-11T20:43:16.905Z",
  "severity": "Info",
  "component": "COMMAND",
  "id": 51803,
  "context": "conn491216",
  "message": "Slow query",
  "duration_ms": 438,
  "namespace": "letakomat.brochure_branch_search_index",
  "operation": {
    "aggregate": "brochure_branch_search_index"
  },
  "attr": {
    "type": "command",
    "ns": "letakomat.brochure_branch_search_index",
    "command": {
      "aggregate": "brochure_branch_search_index",
      "pipeline": [
        {
          "$geoNear": {
            "near": {
              "type": "Point",
              "coordinates": [
                7.3453,
                51.65598
              ]
            },
            "distanceField": "distance",
            "maxDistance": 50000,
            "key": "geo",
            "query": {
              "country": "de",
              "shop_original_id": {
                "$in": [
                  91
                ]
              }
            }
          }
        },
        {
          "$sort": {
            "distance": 1,
            "brochure.id": -1
          }
        },
        {
          "$limit": 5
        }
      ],
      "allowDiskUse": false,
      "cursor": {},
      "$db": "db",
      "lsid": {
        "id": "UUID(\"4ff8f159-085d-4d46-beaa-f2735f4c3d56\")"
      },
      "$clusterTime": {
        "clusterTime": "Timestamp(1699735388, 1)",
        "signature": {
          "hash": "BinData(0,\"FLIRct0TQQZ3fW3DP6TzTHpieDE=\")",
          "keyId": 7248938050049278000
        }
      }
    },
    "planSummary": "GEO_NEAR_2DSPHERE { geo: \"2dsphere\" }",
    "keysExamined": 27071,
    "docsExamined": 46193,
    "hasSortStage": true,
    "cursorExhausted": true,
    "numYields": 50,
    "nreturned": 5,
    "queryHash": "C0DD2F58",
    "planCacheKey": "750C03CF",
    "reslen": 6872,
    "locks": {
      "Global": {
        "acquireCount": {
          "r": 52
        }
      },
      "Mutex": {
        "acquireCount": {
          "r": 2
        }
      }
    },
    "readConcern": {
      "level": "local",
      "provenance": "implicitDefault"
    },
    "writeConcern": {
      "w": "majority",
      "wtimeout": 0,
      "provenance": "implicitDefault"
    },
    "storage": {
      "data": {
        "bytesRead": 9849090,
        "timeReadingMicros": 10187
      }
    },
    "remote": "10.133.254.56:56988",
    "protocol": "op_msg",
    "durationMillis": 438
  }
}

I found the problem in the end. I was using a PHP framework, and some of the framework methods were respecting the ‘readPreference’ setting in the connection string and some not. I had to adjust the code to read the preference. Once this has been done, everything started to work as expected and my secondary nodes are properly utilized.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.