Skip to content

Instantly share code, notes, and snippets.

@jhahspu
Last active January 3, 2025 06:44
Show Gist options
  • Save jhahspu/dc3452bc7a24e7909e65090cd7845e48 to your computer and use it in GitHub Desktop.
Save jhahspu/dc3452bc7a24e7909e65090cd7845e48 to your computer and use it in GitHub Desktop.
MDB

$facet

  1. match movies by genre.name
  2. movies go in results
  3. categories will contain a set of $genres.name
use('the_base');

function stageMatch(gen) {
  return {
    "$match": {
      "genres.name": gen,
    }
  };
}

function stageFacet() {
  return {
    "$facet": {
      "results": [],
      "categories": [
        {
          "$unwind": "$genres"
        },
        {
          "$group": {
            "_id": 0,
            "all": {
              "$addToSet": "$genres.name"
            }
          }
        }
      ]
    }
  }
}

// remove db.movie.aggregate to see the query, copy/pasta in compass the query and re-generate for any language
db.movie.aggregate([
  stageMatch("War"),
  stageFacet()
])
bson.A{
    bson.D{
        {"$match",
            bson.D{
                {"shop", "ewuk"},
                {"category", "led-lamps"},
            },
        },
    },
    bson.D{
        {"$project",
            bson.D{
                {"slug", 1},
                {"sku", 1},
                {"price", 1},
                {"title", 1},
                {"category", 1},
                {"collection", 1},
            },
        },
    },
    bson.D{
        {"$facet",
            bson.D{
                {"results", bson.A{}},
                {"collections",
                    bson.A{
                        bson.D{{"$unwind", "$collection"}},
                        bson.D{
                            {"$group",
                                bson.D{
                                    {"_id", 0},
                                    {"all", bson.D{{"$addToSet", "$collection"}}},
                                },
                            },
                        },
                    },
                },
            },
        },
    },
}
[
  {
    $match: {
      slug: "prod-1-wera",
    },
  },
  {
    $project: {
      _id: 1,
      slug: 1,
      publish: 1,
      new: 1,
      featured: 1,
      google_category: 1,
      title: 1,
      subtitle: 1,
      intro: 1,
      features: 1,
      box_content: 1,
      sections: 1,
      faq: 1,
      thumbnail: 1,
      mpn: 1,
      gtin: 1,
      pricing: 1,
      youtube_id: 1,
      created_at: 1,
      updated_at: 1,
      brandId: {
        $toObjectId: "$brand_id",
      },
      categoryId: {
        $toObjectId: "$category_id",
      },
      collectionId: {
        $toObjectId: "$collection_id",
      },
    },
  },
  {
    $lookup: {
      from: "brand",
      localField: "brandId",
      foreignField: "_id",
      as: "brand",
    },
  },
  {
    $lookup: {
      from: "category",
      localField: "categoryId",
      foreignField: "_id",
      as: "category",
    },
  },
  {
    $lookup: {
      from: "collection",
      localField: "collectionId",
      foreignField: "_id",
      as: "collection",
    },
  },
]
func (mdbs *mdbSettings) UpdateUser(email string, updates map[string]interface{}) (*models.User, error) {
	if len(updates) == 0 {
		return nil, errors.New("nothing to update..")
	}

	ctx, cancel := context.WithTimeout(context.Background(), mdbs.timeout)
	defer cancel()

	collection := mdbs.client.Database(mdbs.database).Collection(userCollection)

	updates["updated_at"] = time.Now().In(mdbs.location)

	filter := bson.M{"email": email}
	updateQuery := bson.M{"$set": updates}

	result, err := collection.UpdateOne(ctx, filter, updateQuery)
	if err != nil {
		return nil, err
	}

	if result.ModifiedCount != 1 {
		return nil, errors.New("an error occured, notify [email protected]")
	}

	updatedUser, err := mdbs.CheckUserEmail(email)
	if err != nil {
		return nil, err
	}

	return updatedUser, err
}
updates := map[string]interface{}{
	"verification_code": "new-verification-code",
	"verified":          true,
	"organization_id":   "new-organization-id",
}

err := UpdateUser(ctx, userCollection, userID, updates)
if err != nil {
	log.Fatalf("Failed to update user: %v", err)
}

$Facet example by CGPT

package main

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	// Set up a connection to the MongoDB server
	client, err := mongo.NewClient("mongodb://localhost:27017")
	if err != nil {
		fmt.Println(err)
		return
	}
	err = client.Connect(context.TODO())
	if err != nil {
		fmt.Println(err)
		return
	}

	// Select the "sales" collection
	collection := client.Database("mydb").Collection("sales")

	// Create the facet pipeline
	pipeline := []bson.M{
		{
			"$facet": bson.M{
				"region_totals": []bson.M{
					{
						"$group": bson.M{
							"_id":   "$region",
							"total": bson.M{"$sum": "$amount"},
						},
					},
					{
						"$sort": bson.M{
							"total": -1,
						},
					},
				},
				"product_totals": []bson.M{
					{
						"$group": bson.M{
							"_id":   "$product",
							"total": bson.M{"$sum": "$amount"},
						},
					},
					{
						"$sort": bson.M{
							"total": -1,
						},
					},
				},
			},
		},
	}

	// Execute the facet pipeline
	cursor, err := collection.Aggregate(context.TODO(), pipeline, options.Aggregate().SetAllowDiskUse(true))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer cursor.Close(context.TODO())

	// Print the results
	for cursor.Next(context.TODO()) {
		var result bson.M
		if err := cursor.Decode(&result); err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(result)
	}
}

$lookup

{
  from: "users",
  localField: "manager_id",
  foreignField: "_id",
  as: "manager"
}

$unwind

{
  path: "$manager",
  includeArrayIndex: 'string',
  preserveNullAndEmptyArrays: true
}

$project

{
  name: 1,
  description: 1,
  site_location: 1,
  "manager.name": "$manager.name",
  "manager.email": "$manager.email",
  "manager.role": "$manager.role"
}
package main
import (
    "context"
    "fmt"
    "log"
    "time"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
    // Set up the MongoDB client
    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
    if err != nil {
        log.Fatal(err)
    }
    defer client.Disconnect(context.TODO())
    // Ping the primary to verify connection
    if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
        log.Fatal(err)
    }
    // Get a handle on the database
    db := client.Database("user_data")
    // Create options for the time series collection
    tso := options.TimeSeries().SetTimeField("timestamp").SetMetaField("userId")
    opts := options.CreateCollection().SetTimeSeriesOptions(tso)
    // Create the time series collection
    if err := db.CreateCollection(context.TODO(), "userStatus", opts); err != nil {
        log.Fatal(err)
    }
    fmt.Println("Time series collection created successfully")
}
package main
import (
    "context"
    "fmt"
    "log"
    "time"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
    // Set up the MongoDB client
    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
    if err != nil {
        log.Fatal(err)
    }
    defer client.Disconnect(context.TODO())
    // Ping the primary to verify connection
    if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
        log.Fatal(err)
    }
    // Get a handle on the database and collection
    db := client.Database("user_data")
    collection := db.Collection("userStatus")
    // Define the document to insert
    doc := bson.D{
        {Key: "userId", Value: "user123"},
        {Key: "location", Value: bson.D{
            {Key: "Latitude", Value: 40.7128},
            {Key: "Longitude", Value: -74.0060},
        }},
        {Key: "status", Value: "active"},
        {Key: "timestamp", Value: time.Now()},
    }
    // Insert the document into the collection
    result, err := collection.InsertOne(context.TODO(), doc)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Inserted document with ID: %v
", result.InsertedID)
}

Local instance

docker pull mongo

docker network create mdb-ecs-network

docker run -d --name mongodb --network mdb-ecs-network -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password mongo

PERSIST DATA
docker run -d --name mongodb --network mdb-ecs-network -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password -v mongodb_data:/data/db mongo

connectionString := "mongodb://admin:password@localhost:27017"

Products by Category with pagination

func (mr *mongoRepository) ProductsByCategory(category, page string) (models.CategoryPage, error) {

	ctx, cancel := context.WithTimeout(context.Background(), mr.timeout)
	defer cancel()

	collection := mr.client.Database(mr.database).Collection("product")

	var products models.SimpleProducts
	var categoryPage models.CategoryPage
	opts := options.Find()

	filter := bson.M{}
	filter["shop"] = "ewuk"

	if category == "all" {
		filter["title"] = bson.M{"$gte": ""}
	} else if category == "featured" {
		now := time.Now()
		year, month, day := now.Date()
		today := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
		filter["featured"] = bson.M{"$gte": today}
	} else {
		filter["category"] = category
	}

	total, err := collection.CountDocuments(ctx, filter)
	if err != nil {
		log.Println(err.Error())
	}

	p, _ := strconv.Atoi(page)
	if p < 1 {
		p = 1
	}
	var perPage int64 = 10

	opts.SetSort(bson.D{{Key: "category", Value: 1}, {Key: "title", Value: 1}})
	opts.SetSkip((int64(p) - 1) * perPage)
	opts.SetLimit(perPage)

	cursor, err := collection.Find(ctx, filter, opts)
	if err != nil {
		return categoryPage, nil
	}

	if err = cursor.All(ctx, &products); err != nil {
		return categoryPage, nil
	}

	lastpage := float64(total) / float64(perPage)

	var link models.Link

	switch category {
	case "all":
		link.DisplayName = "All Products"
	case "featured":
		link.DisplayName = "Featured Products"
	default:
		collection = mr.client.Database(mr.database).Collection("navlinks_ewuk")
		filter = bson.M{"category": category}
		if err := collection.FindOne(ctx, filter).Decode(&link); err != nil {
			return categoryPage, nil
		}
	}

	categoryPage.Products = products
	categoryPage.TotalProducts = total
	categoryPage.CurrentPage = p
	categoryPage.LastPage = int(math.Ceil(lastpage))
	categoryPage.PageTitle = link.DisplayName
	categoryPage.Category = category

	collection = mr.client.Database(mr.database).Collection("product")
	filter = bson.M{"category": category}
	res, err := collection.Distinct(ctx, "brand", filter)
	if err != nil {
		log.Println(err.Error())
	}

	for _, b := range res {
		categoryPage.Brands = append(categoryPage.Brands, b.(string))
	}

	return categoryPage, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment