globalsign / mgo

The MongoDB driver for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use js function in mgo Find request?

thecampagnards opened this issue · comments

In a request I need to use a function :

function(){return this.image.test('%s')}
// request that I want
db.getCollection('images').find(function() { return this.image.test('%s'); })

I tried something like that:

err = r.coll.Find(fmt.Sprintf("function(){return this.image.test('%s')}", image)).One(&t)
// Or
err = r.coll.Find(bson.JavaScript{Code: fmt.Sprintf("return this.image.test('%s');", image)}).One(&t)

But I have this error :

Failed to parse: filter: "function(){return this.image.test('sonarqube%3A6.5-1')}". 'filter' field must be of BSON type object.

Does someone know how to do it, I didn't find anything on stackoverflow or here ?

Thanks

Hi @thecampagnards,

We use the issue tracker to track bugs with mgo - if you have a usage question,
it's best to try Stack Overflow :)

I did a quick search and found this, which might be useful to your case.
https://stackoverflow.com/questions/12008910/how-can-i-ask-mongodb-to-evaluate-some-javascript-in-order-to-obtain-value-for-a

If this doesn't help, I'd suggest to either have a deeper look on stackoverflow or open a question there directly.

Thanks!
Esther