Convert Figma logo to code with AI

globalsign logomgo

The MongoDB driver for Go

1,969
232
1,969
66

Top Related Projects

2,740

The MongoDB driver for Go. UNMAINTAINED - SEE BELOW

The Official Golang driver for MongoDB

15,960

general purpose extensions to golang's database/sql

36,491

The fantastic ORM library for Golang, aims to be developer friendly

5,649

Golang ORM with focus on PostgreSQL features and performance

Quick Overview

mgo (pronounced "mango") is a MongoDB driver for Go. It provides a rich, idiomatic API for working with MongoDB databases in Go applications, offering features like connection pooling, automatic reconnection, and BSON serialization.

Pros

  • Robust and well-tested, with a long history of use in production environments
  • Provides a high-level, idiomatic Go API for MongoDB operations
  • Supports advanced features like GridFS, aggregation pipelines, and transactions
  • Offers good performance and connection pooling out of the box

Cons

  • No longer actively maintained (last commit was in 2018)
  • Not compatible with the official MongoDB Go driver
  • Lacks support for some newer MongoDB features introduced after its last update
  • May have compatibility issues with newer versions of MongoDB

Code Examples

Connecting to MongoDB:

session, err := mgo.Dial("mongodb://localhost:27017")
if err != nil {
    log.Fatal(err)
}
defer session.Close()

Inserting a document:

type Person struct {
    Name string
    Age  int
}

collection := session.DB("test").C("people")
err = collection.Insert(&Person{"John Doe", 30})
if err != nil {
    log.Fatal(err)
}

Querying documents:

var result []Person
err = collection.Find(bson.M{"age": bson.M{"$gt": 25}}).All(&result)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result)

Getting Started

  1. Install mgo:

    go get gopkg.in/mgo.v2
    
  2. Import mgo in your Go code:

    import "gopkg.in/mgo.v2"
    
  3. Connect to MongoDB and start using mgo:

    session, err := mgo.Dial("mongodb://localhost:27017")
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()
    
    // Get a collection
    c := session.DB("test").C("example")
    
    // Perform operations
    err = c.Insert(map[string]string{"name": "John"})
    // ...
    

Remember to handle errors appropriately in your production code.

Competitor Comparisons

2,740

The MongoDB driver for Go. UNMAINTAINED - SEE BELOW

Pros of mgo

  • Original, well-established MongoDB driver for Go
  • Extensive documentation and community support
  • Stable API with long-term compatibility

Cons of mgo

  • No longer actively maintained
  • Lacks support for newer MongoDB features
  • May have unresolved bugs or performance issues

Code Comparison

mgo:

session, err := mgo.Dial("localhost")
if err != nil {
    panic(err)
}
defer session.Close()

globalsign/mgo:

session, err := mgo.DialWithInfo(&mgo.DialInfo{
    Addrs: []string{"localhost"},
    Timeout: 60 * time.Second,
})
if err != nil {
    panic(err)
}
defer session.Close()

Key Differences

  • globalsign/mgo is a fork of the original mgo project
  • globalsign/mgo provides active maintenance and bug fixes
  • globalsign/mgo includes support for newer MongoDB features
  • globalsign/mgo offers improved performance in some areas
  • globalsign/mgo maintains API compatibility with the original mgo

Recommendation

For new projects or those requiring support for recent MongoDB features, globalsign/mgo is generally recommended due to its active maintenance and ongoing improvements. However, existing projects using mgo may continue to use it if they don't require newer features and are satisfied with its stability.

The Official Golang driver for MongoDB

Pros of mongo-go-driver

  • Official MongoDB driver with better long-term support and updates
  • Implements more recent MongoDB features and optimizations
  • Better performance, especially for large datasets and complex operations

Cons of mongo-go-driver

  • Steeper learning curve and more complex API compared to mgo
  • Requires more boilerplate code for basic operations
  • May require significant refactoring when migrating from mgo

Code Comparison

mgo:

session, err := mgo.Dial("mongodb://localhost")
c := session.DB("test").C("users")
err = c.Insert(bson.M{"name": "John", "age": 30})

mongo-go-driver:

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost"))
collection := client.Database("test").Collection("users")
_, err = collection.InsertOne(context.TODO(), bson.M{"name": "John", "age": 30})

The mongo-go-driver code is more verbose but provides better context handling and more flexibility. mgo offers a simpler API for basic operations but lacks some advanced features and optimizations present in the official driver.

15,960

general purpose extensions to golang's database/sql

Pros of sqlx

  • Supports multiple SQL databases (MySQL, PostgreSQL, SQLite)
  • Provides a more flexible and SQL-centric approach
  • Offers both low-level and high-level database operations

Cons of sqlx

  • Requires more manual SQL query writing
  • Less abstraction from underlying database structure
  • May require more setup and configuration for different databases

Code Comparison

mgo example:

session, err := mgo.Dial("localhost")
c := session.DB("test").C("people")
err = c.Insert(&Person{Name: "Alice", Age: 30})

sqlx example:

db, err := sqlx.Connect("postgres", "user=foo dbname=bar")
_, err = db.Exec("INSERT INTO people (name, age) VALUES ($1, $2)", "Alice", 30)

Summary

sqlx is a versatile SQL library supporting multiple databases, offering more flexibility but requiring more manual SQL writing. mgo is MongoDB-specific, providing higher abstraction but limited to one database type. sqlx is better suited for projects using SQL databases and requiring fine-grained control, while mgo excels in MongoDB-centric applications with simpler querying needs.

36,491

The fantastic ORM library for Golang, aims to be developer friendly

Pros of gorm

  • Supports multiple databases (MySQL, PostgreSQL, SQLite, SQL Server)
  • Provides a more feature-rich ORM with advanced querying capabilities
  • Offers hooks for custom logic before/after database operations

Cons of gorm

  • Higher learning curve due to more complex API
  • Potentially slower performance for simple queries compared to mgo
  • May introduce overhead for applications with basic database needs

Code Comparison

mgo example:

session, err := mgo.Dial("localhost")
c := session.DB("test").C("users")
err = c.Insert(&User{Name: "John", Age: 30})

gorm example:

db, err := gorm.Open("mysql", "user:password@/dbname")
user := User{Name: "John", Age: 30}
db.Create(&user)

Key Differences

  • mgo is specifically designed for MongoDB, while gorm supports multiple SQL databases
  • gorm provides a more abstracted ORM layer, whereas mgo offers a lower-level API
  • mgo is generally faster for simple operations, but gorm offers more advanced features and flexibility
5,649

Golang ORM with focus on PostgreSQL features and performance

Pros of pg

  • Supports PostgreSQL-specific features like JSON, JSONB, and array types
  • Offers an ORM-like query builder for more expressive and type-safe queries
  • Provides built-in connection pooling for better performance

Cons of pg

  • Limited to PostgreSQL databases, unlike mgo's MongoDB support
  • Steeper learning curve due to more complex API and ORM-like features
  • Less mature and smaller community compared to mgo

Code Comparison

mgo example:

session, err := mgo.Dial("localhost")
c := session.DB("test").C("users")
err = c.Insert(&User{Name: "John", Age: 30})
var result User
err = c.Find(bson.M{"name": "John"}).One(&result)

pg example:

db := pg.Connect(&pg.Options{Addr: "localhost:5432"})
user := &User{Name: "John", Age: 30}
_, err := db.Model(user).Insert()
err = db.Model(user).Where("name = ?", "John").Select()

Both libraries provide similar functionality for basic database operations, but pg offers a more SQL-like syntax and stronger typing. mgo uses BSON for queries, while pg uses a query builder approach. pg's API is more verbose but potentially more flexible for complex queries.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Build Status GoDoc

The MongoDB driver for Go

This fork has had a few improvements by ourselves as well as several PR's merged from the original mgo repo that are currently awaiting review. Changes are mostly geared towards performance improvements and bug fixes, though a few new features have been added.

Further PR's (with tests) are welcome, but please maintain backwards compatibility.

Detailed documentation of the API is available at GoDoc.

A sub-package that implements the BSON specification is also included, and may be used independently of the driver.

Supported Versions

mgo is known to work well on (and has integration tests against) MongoDB v3.0, 3.2, 3.4 and 3.6.

MongoDB 4.0 is currently experimental - we would happily accept PRs to help improve support!

Changes

  • Fixes attempting to authenticate before every query (details)
  • Removes bulk update / delete batch size limitations (details)
  • Adds native support for time.Duration marshalling (details)
  • Reduce memory footprint / garbage collection pressure by reusing buffers (details, more)
  • Support majority read concerns (details)
  • Improved connection handling (details)
  • Hides SASL warnings (details)
  • Support for partial indexes (details)
  • Fixes timezone handling (details)
  • Integration tests run against MongoDB 3.2 & 3.4 releases (details, more, more)
  • Improved multi-document transaction performance (details, more, more)
  • Fixes cursor timeouts (details)
  • Support index hints and timeouts for count queries (details)
  • Don't panic when handling indexed int64 fields (details)
  • Supports dropping all indexes on a collection (details)
  • Annotates log entries/profiler output with optional appName on 3.4+ (details)
  • Support for read-only views in 3.4+ (details)
  • Support for collations in 3.4+ (details, more)
  • Provide BSON constants for convenience/sanity (details)
  • Consistently unmarshal time.Time values as UTC (details)
  • Enforces best practise coding guidelines (details)
  • GetBSON correctly handles structs with both fields and pointers (details)
  • Improved bson.Raw unmarshalling performance (details)
  • Minimise socket connection timeouts due to excessive locking (details)
  • Natively support X509 client authentication (details)
  • Gracefully recover from a temporarily unreachable server (details)
  • Use JSON tags when no explicit BSON are tags set (details)
  • Support $changeStream tailing on 3.6+ (details)
  • Fix deadlock in cluster synchronisation (details)
  • Implement maxIdleTimeout for pooled connections (details)
  • Connection pool waiting improvements (details)
  • Fixes BSON encoding for $in and friends (details)
  • Add BSON stream encoders (details)
  • Add integer map key support in the BSON encoder (details)
  • Support aggregation collations (details)
  • Support encoding of inline struct references (details)
  • Improved windows test harness (details)
  • Improved type and nil handling in the BSON codec (details, more)
  • Separated network read/write timeouts (details)
  • Expanded dial string configuration options (details)
  • Implement MongoTimestamp (details)
  • Support setting writeConcern for findAndModify operations (details)
  • Add ssl to the dial string options (details)

Thanks to

  • @aksentyev
  • @bachue
  • @bozaro
  • @BenLubar
  • @carldunham
  • @carter2000
  • @cedric-cordenier
  • @cezarsa
  • @DaytonG
  • @ddspog
  • @drichelson
  • @dvic
  • @eaglerayp
  • @feliixx
  • @fmpwizard
  • @gazoon
  • @gedge
  • @gnawux
  • @idy
  • @jameinel
  • @jefferickson
  • @johnlawsharrison
  • @KJTsanaktsidis
  • @larrycinnabar
  • @mapete94
  • @maxnoel
  • @mcspring
  • @Mei-Zhao
  • @peterdeka
  • @Reenjii
  • @roobre
  • @smoya
  • @steve-gray
  • @tbruyelle
  • @wgallagher