func (m *MyMongoContext) RepoCreateFamily(family *models.Family) (id string, err error) { mongoSession := m.Session.Clone() defer mongoSession.Close() c := mongoSession.DB(util.Configuration.MongoDbName).C(familiesCollection) id = util.GetNewId() family.FamilyId = id err = c.Insert(family) if mgo.IsDup(err) { // retry insert with new id m.RepoCreateFamily(family) } else if err != nil { fmt.Errorf("Mongo insert to %s/%s returned '%s'", util.Configuration.MongoDbName, familiesCollection, err.Error()) } return }
// RepoCreateFamily creates a family in the mongo collection func RepoCreateFamily(family *models.Family) (id bson.ObjectId, err error) { mongoSession, err := mgo.Dial(util.Configuration.MongoURL) if err != nil { log.Printf("MongoDB connection failed, with address '%s'.", util.Configuration.MongoURL) } defer mongoSession.Close() c := mongoSession.DB(util.Configuration.MongoDbName).C(familiesCollection) myID := util.GetNewID() family.FamilyID = myID err = c.Insert(family) if mgo.IsDup(err) { // retry insert with new id RepoCreateFamily(family) } else if err != nil { log.Printf("Mongo insert to %s/%s returned '%s'", util.Configuration.MongoDbName, familiesCollection, err.Error()) } return myID, err }