SyntaxError: invalid property id - MongoDB and BASH

Viewed 37

I have db-survey and collection-details. On the terminal when I write-

(echo "use survey"; echo "db.details.find({age:10})") | mongo > age_ten.txt

It gives information of people whose age is 10 in age_ten.txt file.

But when I write-

(echo "use survey"; echo "db.details.find({age:{$gt:60}})") | mongo > medical.txt

It creates the file medical.txt but it has the error -

[js] SyntaxError: invalid property id @(shell):1:22

Funnily when I write - db.details.find({age:{$gt:60}} in mongo enviornment.It gives me the result that I want.

Can someone please explain me what I am doing wrong? Thanks in advance.

1 Answers

Can someone please explain me what I am doing wrong?

The $ in the MongoDB greater than operator is interpreted by bash and the operator consumed. You can see this if you
echo "db.details.find({age:{$gt:60}})"

To prevent this, use ' rather than ".

Related