You are comparing this.items.sku
to a variable mmm
, which isn't initialized and thus has the value unefined
. What you want to do, is iterate the array and compare each entry to the string 'mmm'
. This example does this by using the array method some which returns true
, when the passed function returns true
for at least one of the entries:
{$where:"return this.items.some(function(entry){return entry.sku =='mmm'})"}
But really, don't do this. In a comment to the answer by JohnnyHK you said "my service is just a interface between user and mongodb, totally unaware what the field client want's to store". You aren't really explaining your use-case, but I am sure you can solve this better.
- The $where operator invokes the Javascript engine even though this trivial expression could be done with a normal query. This means unnecessary performance overhead.
- Every single document in the collection is passed to the function, so when you have an index, it can not be used.
- When the javascript function is generated from something provided by the client, you must be careful to sanetize and escape it properly, or your application gets vulnerable to code injection.