0

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?

Marco
  • 1,051
  • 19
  • 41
  • 1
    The syntax error isn't helpful in this case. If you've cut & paste exactly, the problem is that you have [smart quotes](https://en.wikipedia.org/wiki/Quotation_mark#Curved_quotes_within_and_across_applications) (aka curly quotes) around values instead of the normal `"` characters. If you replace `“` and `”` with straight `"` there are no syntax errors. 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. – Stennie May 22 '19 at 22:45
  • Thank you so much. I didn't notice that at all. Yes, that was the issue. @Stennie Everything is working as expected. I upvoted your comment but unable to accept it as an answer as it is a comment, not an answer. If you enter an answer, I will be happy to accept it as the answer so you get that credit. – Marco May 23 '19 at 01:14

1 Answers1

1

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.

Stennie
  • 63,885
  • 14
  • 149
  • 175