Reputation: 360
I'm building a simple query for my MongoDB Collection. The model looks like this:
{
a: [],
user: {
email: String,
name: String,
surname: String
}
}
What I need to get from the query are all the documents that have 'a' as non-empty array and the 'user.email' set. My query is:
Document.find({
a: {
$exists: true,
$not: {
$size: 0
}
},
user: {
$exists: true,
'email': {
$exists: true
}
}
})
The problem is when i perform the query, my server returns a 500 Error saying:
{
"error": {
"name": "MongoError",
"message": "unknown operator: email",
"waitedMS": 0,
"ok": 0,
"errmsg": "unknown operator: email",
"code": 2
}
}
Where am I going wrong? Can anyone help me with this? Thank you!
Upvotes: 2
Views: 3985
Reputation: 5908
Because of the way you structured your query, the MongoDB engine tries parsing the email
keyword as an operator on the user
field.
If you want to check if a nested attribute is defined on a document, you can use the dot notation:
Document.find({
a: {
$exists: true,
$not: {
$size: 0
}
},
"user.email": {
$exists: true
}
})
Upvotes: 4