Regarding this.capital is undefined in mongodb

24 views
Skip to first unread message

Giridhar Bhageshpur

unread,
Apr 14, 2016, 2:19:24 AM4/14/16
to mongodb-user
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

Markus Thielsch

unread,
Apr 29, 2016, 3:07:26 AM4/29/16
to mongodb-user

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 this

thnx,
Giridhar

Reply all
Reply to author
Forward
0 new messages