MongoDB with Python on Google Colab - syntax error on $elemMatch
Answer a question I have installed MongoDB inside the Google Colab VM, created a database, loaded a table and running quite a few commands successfully. When I try to run the following command db.rest
Answer a question
I have installed MongoDB inside the Google Colab VM, created a database, loaded a table and running quite a few commands successfully.
When I try to run the following command
db.restaurants.find({grades : { $elemMatch:{"score":{$gt : 90}}}})
i get the following error:
File "<ipython-input-28-86e6fcc5b428>", line 1
db.restaurants.find({grades : { $elemMatch:{"score":{$gt : 90}}}})
^
SyntaxError: invalid syntax
I have looked at the documentation but I cannot find what is wrong. In fact, I copied a similar command from the documentation itself
db.survey.find(
{ results: { $elemMatch: { product: "xyz" } } }
)
and I got the exact same error!!
File "<ipython-input-33-8cd8460d9613>", line 2
{ results: { $elemMatch: { product: "xyz" } } }
^
SyntaxError: invalid syntax
Now I am thoroughly puzzled. While doing the cut-and-paste, I have taken the trouble to paste in notepad to remove the possibility of hidden, non-printing characters.
What could be the issue here? Please help with any suggestions.
EDITS : I am using python and pymongo package and my colab notebook is available here.
Answers
You need to put the JSON keys in quotes for python objects.
db.restaurants.find({"grades" : { "$elemMatch":{"score":{"$gt" : 90}}}})
更多推荐
所有评论(0)