Search code examples
mongodbmongodb-replica-set

How to display line # in Mongodb Shell- ReplicaSet config Error


I'm testing a replicaSet on my local machine. It's only for testing. I started my local mongodb instance bin/mongod Then started three instances with the following configuration:

mongod --replSet rstest --logpath \data\rs2\2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64 

mongod --replSet rstest --logpath \data\rs3\3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64 

mongod --replSet rstest --logpath \data\rs3\3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64 

Then I started mongo --port 27017 and type the following configuration:

config = {_id:“rstest”, members:[
{_id:0,host:“localhost:27017”},
{_id:1,host:“localhost:27018”},
{_id:2,host:“localhost:27019”}
]};

When I do type the above code and hit enter, I receive the following error message: E QUERY [js] SyntaxError: missing : after property id @(shell):1:101

E QUERY    [js] SyntaxError: missing : after property id @(shell):1:101

I'm unable to figure out where is the miss : Is there a way to get the shell screen to display line #s? Or where is that 1:101

Any idea why I'm receiving this error? Where is the missing : should go?


Solution

  • The problem is that you have smart quotes (aka curly quotes) around values instead of the normal " characters. If you replace and with straight " there are no syntax errors.

    I'm unable to figure out where is the miss : Is there a way to get the shell screen to display line #s? Or where is that 1:101

    The error message indicates line 1, column 101 but the syntax error isn't particularly helpful because smart quotes have confused the JavaScript interpreter.

    Unfortunately there isn't an option to turn on line numbers in the mongo shell so you'd have to use an external editor or count the lines & character offset in the error message. Ideally you should use an editor that includes JavaScript syntax checking and line numbering.

    There are a few ways to conveniently work with an external editor in the mongo shell:

    1) Set the EDITOR environment variable before starting the mongo shell and use the edit command to modify a shell variable using external editor.

    For example:

    export EDITOR=vim
    mongo
    
    > var cfg = {}
    > edit cfg
    

    The edit command in the shell creates a temporary file that is eval'd in the mongo shell when you quit your external editor. If there are any syntax errors your change will not be saved, so this is better for quick changes than extended coding.

    2) Save your JavaScript in a file using an external editor and use the load() command in the mongo shell:

    load("/path/to/myfile.js")
    

    This approach is more convenient for working with larger snippets of JavaScript since you do not have to worry about syntax errors preventing your changes from being saved.