Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: invalid property id - MongoDB and BASH

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.

like image 634
NightFury Avatar asked Mar 20 '26 14:03

NightFury


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 ".

like image 81
Armali Avatar answered Mar 23 '26 02:03

Armali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!