问题
I am from an Oracle PLSQL background and I have just started learning MongoDB commands.
>db.Employee.find()
{ "_id" : 10, "EmployeeName" : "Smith" }
{ "_id" : 20, "EmployeeName" : "Nandhi" }
{ "_id" : 30, "EmployeeName" : "Rajan" }
{ "_id" : 50, "EmployeeName" : "Raju" }
I need to query the documents like:
WHERE EmployeeName = "Smith" OR (EmployeeName = "Rajan" AND _id = 30);
I have written the corresponding code as below:
db.Employee.find({$or:[{"EmployeeName":"Smith",{"EmployeeName":"Rajan","_id":30}}]})
Error Message
2019-03-13T22:48:26.123+0530 E QUERY [js] SyntaxError: invalid property id @(shell):1:47
回答1:
Your $or
terms need to be in separate objects:
db.Employee.find({$or:[{"EmployeeName":"Smith"}, {"EmployeeName":"Rajan", "_id":30}]})
回答2:
Braces are incorrectly enclosed. Use like this
db.Employee.find({$or:[{"EmployeeName":"Smith"},{"EmployeeName":"Rajan","_id":30}]})
来源:https://stackoverflow.com/questions/55147865/syntaxerror-invalid-property-id-mongodb