go mongox Library: Easily Construct BSON Data for MongoDB
Introduction
When developing Go applications that use MongoDB, we often need to write data in BSON format. Writing simple BSON data is straightforward, but dealing with complex BSON documents can be time-consuming and error-prone. Small mistakes or omissions may lead to unexpected results, increasing development difficulty and debugging time.
In this context, go-mongox
was born. go-mongox
is a generics-based library that extends MongoDB's official Go driver, providing features like:
Generic MongoDB collections
CRUD operations for documents
Aggregation operations
Built-in basic
Model
structs with automated updates for default fieldsSupport for BSON data construction
Struct tag validation
Built-in hooks
Plugin-based programming support
This article focuses on the BSON Data Construction module of go-mongox
.
GitHub repository: https://github.com/chenmingyong0423/go-mongox
Installation
Run the following command to install the go-mongox
module in your Go application:
go get github.com/chenmingyong0423/go-mongox
Builder
go-mongox
provides various BSON builders and functions, making it easy for developers to construct BSON data for different scenarios, whether for queries, updates, or complex aggregation operations. These builders and functions, available in packages like bsonx
, query
, update
, and aggregation
, optimize the code-writing process and significantly improve development efficiency. They make handling complex BSON data simpler and more efficient.
Query Construction - query
Package
The query
package offers convenient methods for constructing MongoDB query conditions. It includes a range of functions and builders to simplify query construction. Builders allow flexible, chainable calls for complex queries, while functions can be used for quick construction of simpler queries.
Simple Query Construction
For individual query conditions, we can directly use functions:
/*
{
"_id": "1024"
}
*/
query.Id("1024")
/*
{
"name": {
"$in": ["Mingyong Chen", "chenmingyong"]
}
}
*/
query.In("name", "Mingyong Chen", "chenmingyong")
/*
{
"age": {
"$gte": {
"$numberInt": "18"
}
}
}
*/
query.Gte("age", 18)
/*
{
"name": {
"$regex": ".*cmy.*",
"$options": "i"
}
}
*/
query.RegexOptions("name", ".*cmy.*", "i")
These functions streamline code-writing and improve efficiency.
For more usage details, check the official documentation: Query Package | go-mongox.
Complex Query Construction
For more complex BSON data, we can use builders:
/*
{
"age": {
"$gte": {
"$numberInt": "18"
},
"$lte": {
"$numberInt": "25"
}
},
"name": {
"$in": ["Mingyong Chen", "chenmingyong"]
}
}
*/
query.NewBuilder().Gte("age", 18).Lte("age", 25).In("name", "Mingyong Chen", "chenmingyong").Build()
/*
{
"lastLogin": {
"$gte": {
"$date": {
"$numberLong": "1729670489965"
}
}
},
"$or": [
{ "status": { "$eq": "active" } },
{ "loginAttempts": { "$gte": { "$numberInt": "5" } } }
]
}
*/
query.NewBuilder().Gte("lastLogin", time.Now().Add(-30*24*time.Hour)).Or(
query.Eq("status", "active"),
query.Gte("loginAttempts", 5),
).Build()
/*
{
"name": {
"$eq": "Mingyong Chen"
},
"hobbies": {
"$elemMatch": {
"name": {
"$eq": "coding"
},
"level": {
"$gte": {
"$numberInt": "5"
}
}
}
}
}
*/
query.NewBuilder().
Eq("name", "Mingyong Chen").
ElemMatch("hobbies", query.NewBuilder().Eq("name", "coding").Gte("level", 5).Build()).
Build()
The builder's chainable API makes constructing complex queries straightforward, improving code readability and maintainability.
For more usage details, check the official documentation: Query Package | go-mongox.
Update Document Construction - update
Package
The update
package simplifies the construction of MongoDB update documents. It includes a range of functions and builders for both simple and complex update scenarios.
Simple Update Construction
For individual updates, we can directly use functions:
/*
{
"$set": {
"name": "Mingyong Chen"
}
}
*/
update.Set("name", "Mingyong Chen")
/*
{
"$inc": {
"money": {
"$numberInt": "100000"
}
}
}
*/
update.Inc("money", 100000)
/*
{
"$push": {
"tags": "golang"
}
}
*/
update.Push("tags", "golang")
For more usage details, check the official documentation: Update Package | go-mongox.
Complex Update Construction
Builders are used for more complex updates:
/*
{
"$set": {
"name": "Mingyong Chen",
"age": {
"$numberInt": "18"
}
}
}
*/
update.NewBuilder().Set("name", "Mingyong Chen").Set("age", 18).Build()
/*
{
"$set": {
"update_at": {
"$date": {
"$numberLong": "1732262905124"
}
}
},
"$inc": {
"view": {
"$numberInt": "1"
}
}
}
*/
update.NewBuilder().Set("update_at", time.Now()).Inc("view", 1).Build()
For more usage details, check the official documentation: Update Package | go-mongox.
Aggregation Pipeline and Expression Construction - aggregation
Package
The aggregation
package provides tools for constructing MongoDB aggregation pipelines. It supports both pipeline stages (e.g., $group
, $match
) and expressions (e.g., $add
, $subtract
), simplifying complex aggregation workflows.
Pipeline Stages
Use aggregation.StageBuilder
to construct pipeline stages. Example:
// Example pipeline construction
aggregation.NewStageBuilder().
Match(query.Eq("status", "active")).
Group("$age", aggregation.NewBuilder().Sum("count", 1).Push("names", "$name").Build()...).
Build()
For more usage details, check the official documentation: Aggregation Stage Builder | go-mongox.
Expressions
Use aggregation.BsonBuilder
for constructing pipeline expressions. Example:
/*
{
"isAdult": {
"$gte": ["$age", {
"$numberInt": "18"
}]
}
}
*/
aggregation.Gte("isAdult", "$age", 18)
/*
{
"birthYear": {
"$subtract": [2024, "$age"]
}
}
*/
aggregation.Subtract("birthYear", 2024, "$age")
For more usage details, check the official documentation: Aggregation Expression Builder | go-mongox.
bsonx
Package: Simplifying BSON Data Construction
The bsonx
package provides utility functions and builders for constructing BSON data more easily.
Builder Example:
/*
{
"name": "Mingyong Chen",
"name": "chenmingyong"
}
*/
bsonx.NewD().Add("name", "Mingyong Chen").Add("name", "chenmingyong").Build()
Function Example:
bsonx.M("name", "Mingyong Chen")
bsonx.E("name", "Mingyong Chen")
bsonx.D("name", "Mingyong Chen")
bsonx.A("Mingyong Chen", "chenmingyong")
For more usage details, check the official documentation: BSONx Package | go-mongox.
Conclusion
This article detailed the BSON Data Construction module of the go-mongox
library. With its variety of builders and functions, go-mongox
empowers developers to construct BSON
data efficiently across diverse scenarios.
Contribute to
go-mongox
!