Group By and Sort in GoLang and MongoDB

Create Database Create a database with the name is learn_mongodb_golang. This database have a collection: Product collection. /* Create learn_mongodb_golang database */ use learn_mongodb_golang /* Create Product collection */ db.createCollection(‘product’); /* Dumping data for `product` collection */ db.getCollection(‘product’).insert({ “name” : “Mobile 1”, “price” : 45.0, “quantity” : 4, “status” : true, “date” : ISODate(“2016-10-20T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa45711668”), “brand” : { “_id” : new ObjectId(), “name” : “brand 1” }, “colors” : [ “red”, “green”, “blue” ] }); db.getCollection(‘product’).insert({ “name” : “Mobile 2”, “price” : 12.0, “quantity” : 7, “status” : true, “date” : ISODate(“2017-11-14T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa45711668”), “brand” : { “_id” : new ObjectId(), “name” : “brand 2” }, “colors” : [ “yellow”, “green” ] }); db.getCollection(‘product’).insert({ “name” : “Mobile 3”, “price” : 28.0, “quantity” : 8, “status” : true, “date” : ISODate(“2017-11-20T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa45711668”), “brand” : { “_id” : new ObjectId(), “name” : “brand 3” }, “colors” : [ “black”, “blue” ] }); db.getCollection(‘product’).insert({ “name” : “Laptop 1”, “price” : 39.0, “quantity” : 12, “status” : false, “date” : ISODate(“2017-12-26T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa45711669”), “brand” : { “_id” : new ObjectId(), “name” : “brand 1” }, “colors” : [ “blue” ] }); db.getCollection(‘product’).insert({ “name” : “Laptop 2”, “price” : 86.0, “quantity” : 23, “status” : true, “date” : ISODate(“2017-03-11T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa45711669”), “brand” : { “_id” : new ObjectId(), “name” : “brand 1” }, “colors” : [ “blue”, “yellow” ] }); db.getCollection(‘product’).insert({ “name” : “Tivi 1”, “price” : 22.0, “quantity” : 7, “status” : true, “date” : ISODate(“2017-06-26T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa4571166a”), “brand” : { “_id” : new ObjectId(), “name” : “brand 1” }, “colors” : [ “blue”, “white”, “black” ] }); db.getCollection(‘product’).insert({ “name” : “Tivi 2”, “price” : 86.0, “quantity” : 23, “status” : false, “date” : ISODate(“2017-09-24T00:00:00.000Z”), “categoryId” : ObjectId(“5a30de130867edfa4571166a”), “brand” : { “_id” : new ObjectId(), “name” : “brand 3” }, “colors” : [ “red”, “green” ] }); Installing Libraries Install MGO library as below: go get gopkg.in/mgo.v2 Configuration In src folder, create new folder named config. In this folder, create new file named config.go, this file is used to connect to mongodb database: package config import ( mgo “gopkg.in/mgo.v2” ) func GetMongoDB() (*mgo.Database, error) { host := “mongodb://localhost:27017” dbName := “learn_mongodb_golang” session, err := mgo.Dial(host) if err != nil { return nil, err } db := session.DB(dbName) return db, nil } Entity Classes Create new folder named entities. In this folder, create new go files as below: Brand Entity In entities folder, create new go file named brand_entity.go as below: package entities import ( “fmt” “gopkg.in/mgo.v2/bson” ) type Brand struct { Id bson.ObjectId `bson:”_id”` Name string `bson:”name”` } func (this Brand) ToString() string { result := fmt.Sprintf(“\nbrand id: %s”, this.Id.Hex()) result = result + fmt.Sprintf(“\nbrand name: … Continue reading Group By and Sort in GoLang and MongoDB