JSON Filter Not Following Suggested Format

I believe in one of the lectures it said that the JSON filter will always follow this syntex:
{“filter” : “example”}, however when the Scavanger hunt labs I did not need to put my “filter” in a string. I used {director: “Patty Jenkins”}. Has the syntax changed since the video was recorded to allow the director part to not be a string?

Compass doesn’t seem to mind whether or not the name of a JSON field is enclosed in quotes (although if the name of the field contains spaces then it will need to be enclosed in qoutes). Interestingly, I’ve just tried going to the schema view of the citibike.trips collection and selecting one of the bars on the visualisation for the birth year field, and the filter string generated by Compass is

{'birth year': {$gte: 1970,$lt: 1975}}

(single quotes rather than double quotes around the field name). And I’ve replaced the single quotes with double quotes, clicked the Analyze button, and that works fine too. So it seems that in Compass, single quotes, double quotes or no quotes at all around field names are all fine (as long as the field name doesn’t contain spaces).

Whether or not field names need quotes in JSON seems to depend on the context in which the JSON is being used, for example when using JSON syntax to instantiate an object within some JavaScript code, quotes aren’t needed. But in a .json configuration file in a Node.js application, no quotes around the field names seems to be treated as an error.

Under the hood I guess it probably depends on how JSON (de)serialization is implemented in whatever context you’re using, and how strict the JSON (de)serializer is in interpreting JSON text.

Always enclosing your field names in quotes is probably a good practice to get into the habit of, even if it’s not required in the context of Compass filters (at the moment).

Hope this helps. (Disclaimer: I’m just a fellow student trying to be helpful, I’m not an expert)


That’s very helpful Simon. Thanks for sharing!

Thanks for sharing. BTW did you realize you don’t even have to click analyze, just change the date and the result get highlighted immediately. When you click analyze it takes some time to refresh.