Hi Giridhar,
I had a look at your code and without knowing your environment I believe you need to use the following code to make it work in the mongo
shell:
db.world.find({"$where":"this.capital == this.name"},{"name":1,"capital":1,"_id":0})
or depending on your use case and dataset you can also use aggregation:
db.world.aggregate( { $project:{ equal:
{ $eq:[ '$capital', '$name' ], capital: "$capital", name:"$name" }
} }, { $match:
{ equal:true }
})
Judging from the error message you receive the function .match()
is trying to be applied to a string variable this.capital
which is not defined in the mongo shell.
For more general information about the usage of $where
within MongoDB have a look at our Evaluation Query Operators documentation.
Regards,
Markus
On Thursday, April 14, 2016 at 4:19:24 PM UTC+10, Giridhar Bhageshpur wrote:
Hi all,I was running this query
db.world.find({"$where":"this.capital.match('^'+this.name+'.+
)"},{"name":1,"capital":1,"_id":0}) on mongo shell. I got the error "TypeError: this.capital is undefined" Please let me know how to resolve thisthnx,Giridhar