Re: Regarding this.capital is undefined in mongodb

From: Markus Thielsch <markus.thielsch@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Fri, 29 Apr 2016 00:07:25 -0700 (PDT)
Why ads?


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 
<https://docs.mongodb.org/manual/reference/operator/query/where/#op._S_where>
.

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 
this

thnx,
Giridhar



-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/415df98f-54ab-4384-9394-0580f30de263%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?