When pasting from Compass to Mongo Shell, why does this fail? And what is the fix?

MongoDB Enterprise Cluster0-shard-0:PRIMARY> pipeline = [{$project: {
… “awards.text”: 1,
… “title”: 1,
… “my_rating”:
… {
… “$ifNull”: ["$imdb.rating",0]
… },
… “my_votes”:
… {
… “$ifNull”: ["$imdb.votes",0]
… },


MongoDB Enterprise Cluster0-shard-0:PRIMARY> }}, {$addFields: {
2021-05-27T15:37:32.072-0400 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ‘}’ :
@(shell):1:0
MongoDB Enterprise Cluster0-shard-0:PRIMARY> “scaled_votes”: {
… “$add”: [
… 1,
… {
… “$multiply”: [
… 9,
… {"$divide": [
… { “$subtract”: ["$my_votes", 5] },
… { “$add”: [1521100,0] }
… ]}
… ]
… }
… ]
… }
2021-05-27T15:37:32.094-0400 E QUERY [js] uncaught exception: SyntaxError: unexpected token: ‘:’ :
@(shell):1:14
MongoDB Enterprise Cluster0-shard-0:PRIMARY> ,
… “everything” : 1
2021-05-27T15:37:32.110-0400 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ‘,’ :
@(shell):1:0
MongoDB Enterprise Cluster0-shard-0:PRIMARY> }}, {$addFields: {
2021-05-27T15:37:32.125-0400 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ‘}’ :
@(shell):1:0
MongoDB Enterprise Cluster0-shard-0:PRIMARY> “normalized_rating”: {"$avg": ["$scaled_votes", “$imdb.rating”]}
2021-05-27T15:37:32.141-0400 E QUERY [js] uncaught exception: SyntaxError: unexpected token: ‘:’ :
@(shell):1:19
}}, {$match: {
“awards.text”: { “$regex” : /Oscar/i }
}}, {$project: {
“normalized_rating”: 1, “my_rating” : 1, “title” : 1, “award.text”: 1
}}, {$group: {
“_id”: “$everything”,
“highest_rating”: { “$max” : “$normalized_rating” },
“lowest_rating”: { “$min” : “$normalized_rating” },
“average_rating”: { “$avg” : “$normalized_rating” },
“deviation”: { “$stdDevPop” : “$normalized_rating” },
}}]MongoDB Enterprise Cluster0-shard-0:PRIMAR}}, {$match: {
2021-05-27T15:37:32.160-0400 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ‘}’ :
@(shell):1:0
MongoDB Enterprise Cluster0-shard-0:PRIMARY> “awards.text”: { “$regex” : /Oscar/i }
2021-05-27T15:37:32.178-0400 E QUERY [js] uncaught exception: SyntaxError: unexpected token: ‘:’ :
@(shell):1:13
MongoDB Enterprise Cluster0-shard-0:PRIMARY> }}, {$project: {
2021-05-27T15:37:32.196-0400 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ‘}’ :
@(shell):1:0
MongoDB Enterprise Cluster0-shard-0:PRIMARY> “normalized_rating”: 1, “my_rating” : 1, “title” : 1, “award.text”: 1
2021-05-27T15:37:32.212-0400 E QUERY [js] SyntaxError: unexpected token: ‘:’ :
@(shell):1:19
MongoDB Enterprise Cluster0-shard-0:PRIMARY> }}, {$group: {
2021-05-27T15:37:32.228-0400 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ‘}’ :
@(shell):1:0
MongoDB Enterprise Cluster0-shard-0:PRIMARY> “_id”: “$everything”,
… “highest_rating”: { “$max” : “$normalized_rating” },
… “lowest_rating”: { “$min” : “$normalized_rating” },
… “average_rating”: { “$avg” : “$normalized_rating” },
… “deviation”: { “$stdDevPop” : “$normalized_rating” },
… }}]
2021-05-27T15:37:33.323-0400 E QUERY [js] uncaught exception: SyntaxError: unexpected token: ‘:’ :
@(shell):1:5
MongoDB Enterprise Cluster0-shard-0:PRIMARY>

And to clarify, I’m not putting any characters into this that Compass wasn’t already cool with.

Hello !

Your problems comes from the typographic double quotes. Replace with straight double quotes.

How odd that working Compass code somehow got corrupted on Mac. I’m aware of Mac doing this with text, if one is not careful - I’m just surprised that I had a working Compass pipeline and somehow it still got modified.

Did you get this to run? I am seeing more errors in the code, but that may be because I am using VS Code for mac.

If you got it to run, can you post it with ``` before and after your query? That will make the output like the first response you got. I would like to see the entire query.

Hi !
No I ran the aggregation once to check the syntax. I didn’t worked on the logic. My return value was 0…

Sorry, I should have marked the solution earlier. Wasn’t really concerned about results as much as the ability to run the query.

So what I’ve seemed to discover is that in certain limited cases, Mac OS X will convert the standard quotes to open and closed quotes without any intervention. There might be some buffer size where it’s assumed to be regular text or some check. IDK.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.