0

I was trying to search a collection with multiple field conditions using $elemMatch operator. Then I encountered an error "too many arguments in call to c.Find". Document structure is as follows:-

{
    "_id" : ObjectId("56cfca4bf23e4e2859257425"),
    "company_name" : "bank",
    "admin" : {
        "email" : "xyz@bank.com",
        "fullname" : "xyz"
    },
    "process" : [ 
        {
            "process_name" : "Enquiry",
            "processtype" : 0,
            "sortorder" : 0
        }, 
        {
            "process_name" : "Converted",
            "processtype" : 1,
            "sortorder" : 1
        }
    ]
}

My query is as follows:-

colQuerier := bson.M{"company_name": "bank"}
match := bson.M {"process": bson.M {"$elemMatch": bson.M{"process.process_name":"Converted"}}}
err := c.Find(colQuerier,match).Sort("-id").All(&results)

What is wrong with my query? Is it any syntax error?

Arjun Ajith
  • 1,850
  • 5
  • 21
  • 46

1 Answers1

2

Because c.Find(https://godoc.org/gopkg.in/mgo.v2#Collection.Find) only allow one param

You can try

query := bson.M{
    "company_name": "bank",
    "process": bson.M{
        "$elemMatch": bson.M{"process.process_name":"Converted"}
    }
}
err := c.Find(query).Sort("-id").All(&results)
Specode
  • 973
  • 9
  • 19
  • Thanks bro, but its returning errors like:- backend/models/processmodel.go:30: syntax error: unexpected :, expecting ) backend/models/processmodel.go:31: non-declaration statement outside function body any idea???? – Arjun Ajith Feb 26 '16 at 09:55
  • //First line is line 30 //c.Find(bson.M{"company_name": "bank"},"process": bson.M {"$elemMatch": bson.M{"process.process_name":"Converted"}}) //colQuerier = bson.M{"company_name": Company} // change := bson.M {"$push": bson.M {"process": Process[0].ProcessItemMinimal}} // err := c.Update(colQuerier,change) @Specode.. Sorry for the delay man. thanks for you help. First line is the query you gave me. I tried it in the way you gave me. but it too didn't work. – Arjun Ajith Feb 29 '16 at 03:53
  • Try to fix line 30 to: c.Find(bson.M{"company_name": "bank","process": bson.M {"$elemMatch": bson.M{"process.process_name":"Converted"}}}) – Specode Feb 29 '16 at 14:35