Bye
Bye

Reputation: 768

Rails: mongodb error when using $lt

this code return an error

db[:zips].find(city: {$lt: 'd'}).limit(2).to_a.each{|r| pp r}

syntax error, unexpected '}', expecting end-of-input

However, this code works well

db[:zips].find(city: {:$lt=> 'd'}).limit(2).to_a.each{|r| pp r}

Why can not use :$lt like the first one?

Upvotes: 1

Views: 103

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84124

You can't use the JSON-like {key: value} syntax in this case , because the key starts with $. Either use the older hash syntax or, since ruby 2.2,

{'$lt': 'd'}

I couldn't find a reference for when quoting is required (emojis are OK for example) - I suspect you would have to delve into the ruby source for this.

Upvotes: 1

Related Questions