snøreven
snøreven

Reputation: 1974

Retrieve from database via mgo using interface

I have following (untested) function:

func Execute(task MyInterface) {
    db := session.DB(task.Database()).C(task.Collection())
    var tasks []MyInterface
    db.Find(nil).All(&tasks)
    for _, t := range tasks { t.Do() }
}

I want to give my function a struct that implements interface MyInterface. It should then retrieve some values from mongodb and call a method on the retrieved structs.

Problem is error: reflect.Set: value of type bson.M is not assignable to type mypkg.MyInterface - is there some way to make this work? I tried to instantiate tasks using the type of the original struct, but couldn't get it working.

Any help would be appreciated - thanks :)

Upvotes: 1

Views: 2152

Answers (1)

JimPaek
JimPaek

Reputation: 145

mgo can't create MyInterface objects since MyInterface is just an interface. Try doing :

myObject := &MyInterface{}  //fails

I think it's basically what mgo is trying to do for each record it tries to unmarshal.

To achieve what you want, make tasks of type []map[string]interface{} or []bson.M (the latter is an alias for the former) and then:

for _, o := range tasks {
    t := &MyTypeThatImplementsMyInterface{ 
        Field1 : o["my_field"],
        //etc.  
    } 
    t.Do()
}

Either that or make tasks a slice of objects of a type (var tasks []MyStructType) that implements MyInterface and that its fields match the keys and values types of your bson objects in your database. That might not work if your stored data is of more than one type implementing MyInterface, hence the slice of maps option above.

Also, be cautious with Find(nil).All(&tasks). If you have millions of records in that collection, depending on the records size, you may fall short of memory.

Upvotes: 1

Related Questions