Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1677

mongosh version 2.1.1 is failing with getPlanCache().clear() command

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.0
    • Affects Version/s: 2.1.5
    • Component/s: None
    • Environment:
      OS:
      node.js / npm versions:
      Additional info:
    • Developer Tools
    • Not Needed

      This is a ticket based on field report, the issue described seems to be a genuine bug:

      Description

      What is going wrong?

      Hi Team,

      I am executing getPlanCache().clear() after connecting to secondary node directly but it's failing with mongosh version 2.1.1 }}but it's successful with {{1.10.6

      MongoDB Version used : 6.0.12

      Please find the same command executed with mongosh version 2.1.1 and 1.10.6 where 2.1.1 is failing while executing getPlanCache().clear()

      Mongosh version: 2.1.1

      Connecting to: mongodb://<credentials>@cluster0-shard-00-00.gxj7d.mongodb.net:27017/?directConnection=true&authSource=admin&tls=true&appName=mongosh+2.1.1
      
      Using MongoDB: 6.0.12
      
      Using Mongosh: 2.1.1
      
      For mongosh info see: [https://docs.mongodb.com/mongodb-shell/]
      
      (node:56661) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
      
      (Use `node --trace-deprecation ...` to show where the warning was created)
      
      Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
      
      You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] test> use yelp
      
      switched to db yelp
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.getMongo().setReadPref('secondary')
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.reviews.getPlanCache().list()
      
      []
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.reviews.getPlanCache().clear()
      
      MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string
      
      Mongosh version: 1.10.6
      
      Connecting to: mongodb://<credentials>@cluster0-shard-00-00.gxj7d.mongodb.net:27017/?directConnection=true&authSource=admin&tls=true&appName=mongosh+1.10.6
      
      Using MongoDB: 6.0.12
      
      Using Mongosh: 1.10.6
      
      mongosh 2.1.1 is available for download: [https://www.mongodb.com/try/download/shell]
      
      For mongosh info see: [https://docs.mongodb.com/mongodb-shell/]
      
      Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
      
      You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] test> use yelp
      
      switched to db yelp
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.reviews.getPlanCache().list()
      
      MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.getMongo().setReadPref('secondary')
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.reviews.getPlanCache().list()
      
      []
      
      Atlas atlas-f5qtqf-shard-0 [direct: secondary] yelp> db.reviews.getPlanCache().clear()
      
      {
      
      ok: 1,
      
      '$clusterTime': {
      
      clusterTime: Timestamp(
      
      { t: 1702995099, i: 1 }
      
      ),
      
      signature:
      
      { hash: Binary(Buffer.from("3e8c6f8c4177c0ee9d98bb57d62316a5ce0f643a", "hex"), 0), keyId: Long("7272777850141278209") }
      
      },
      
      operationTime: Timestamp(
      
      { t: 1702995099, i: 1 }
      
      )
      
      }
      
       

      Steps to Reproduce

      Use a MongoDB replica set with 6.0.12
      Connect to the secondary node directly with mongosh version: 2.1.1 without passing any information related to replica set.
      Execute db.getMongo().setReadPref('secondary') so we can execute queries against secondary node.
      use any database and collection in mongosh where you have some data and execute db.<collectionname>.getPlanCache().clear() . This will generate an error that we can't perform operation against secondary node.

      Expected Results

      Command should be successful as we have allowed queries against secondary node using db.getMongo().setReadPref('secondary'). We are seeing the expected results in mongosh 1.10.6
      Actual Results

      We are getting error:

      MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string

            Assignee:
            Unassigned Unassigned
            Reporter:
            sergey.petushkov@mongodb.com Sergey Petushkov
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: