mongodbmongo-shell

mongo shell, how do I direct queries to the secondary


I'm very new to mongo and I'd like to direct some queries to the secondaries from the mongo shell.

I've read the docs which say to set the read preference.

Specifically

In the mongo shell, the readPref() cursor method provides access to read preferences.

I've read the docs on the readPref cursor method (here if you're interested: http://docs.mongodb.org/manual/reference/method/cursor.readPref/#cursor.readPref) however I can't make it work.

mongos> cursor.readPref(secondary);
2015-10-15T10:03:47.480+0000 E QUERY    ReferenceError: cursor is not defined
    at (shell):1:1

readPref(secondary);
2015-10-15T10:04:16.224+0000 E QUERY    ReferenceError: secondary is not defined
    at (shell):1:10

mongos> readPref('secondary');
2015-10-15T10:04:37.848+0000 E QUERY    ReferenceError: readPref is not defined
    at (shell):1:1

I've tried every combination of things I can think of, but always get a ReferenceError.

What am I missing / not understanding?


Solution

  • From within mongosh:

    db.getMongo().setReadPref('secondary')
    

    Reference: https://www.mongodb.com/docs/manual/reference/method/Mongo.setReadPref/#specify-read-preference-mode