A fast and simple
MongoDB driver for Go

go get -u github.com/golungo/lungo

Writing MongoDB aggregations can be very time-consuming.
That's why lungo exists

Connect
main.go
Define
models/model.go
Use
handlers/handler.go
package main
  
import (
  "os"
  
  "github.com/golungo/lungo"
)
  
func main() {
  uri := os.GetEnv("MONGO_URI")
  if err := lungo.Init(uri); err != nil {
    panic(err)
  }
  
  if err := lungo.Connect(); err != nil {
    panic(err)
  }
  
  defer func() {
    if err := lungo.Disconnect(); err != nil {
      panic(err)
    }
  }()
}
package models
    
import (
  "reflect"
      
  "github.com/golungo/lungo"
  "github.com/golungo/lungo/query"
)
    
type Child struct {
  ID          lungo.ObjectID `json:"_id" bson:"_id"`
  Title       string         `json:"title" bson:"title"`
}
    
type Parent struct {
  query.Model `json:"-" bson:"-"`
  ID          lungo.ObjectID `json:"_id" bson:"_id"`
  ChildID     lungo.ObjectID `json:"childId" bson:"childId"`
  Child       []Child        `json:"child" bson:"-"`
}
    
func GetCollection() query.Model {
  var model Parent
     
  return model.Init(
    "parents", reflect.TypeOf(model),
  ).Virtual(
    "childs", "childId", "_id", "child", false,
  )
} 
package handlers
    
import (
  "encoding/json"
    
  "golungo/example/models"
    
  "github.com/golungo/lungo"
)
          
func Search(objectID lungo.ObjectID) ([]models.Parent, error) {
  var Parents []models.Parent
    
  filter := lungo.filter{
    "_id": objectID
  }
    
  populate := lungo.Populate{
    "childs"
  }
            
  result, err := models.GetCollection().Match(filter).Lookup(populate).Limit(limit).Exec()
  if err != nil {
    return Parents, err
  }
            
  if err := json.Unmarshal(result, &Parents); err != nil {
    return Parents, err
  }
            
  return Parents, nil
}