https://github.com/eshork/go-mongoid
a (sort of) reimplementation of Mongoid for Go, using mongo-go-driver
https://github.com/eshork/go-mongoid
go golang mongo mongodb mongoid odm orm
Last synced: 5 months ago
JSON representation
a (sort of) reimplementation of Mongoid for Go, using mongo-go-driver
- Host: GitHub
- URL: https://github.com/eshork/go-mongoid
- Owner: eshork
- License: mit
- Created: 2019-07-21T22:58:37.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-11-02T00:57:34.000Z (8 months ago)
- Last Synced: 2025-11-02T02:34:09.354Z (8 months ago)
- Topics: go, golang, mongo, mongodb, mongoid, odm, orm
- Language: Go
- Homepage:
- Size: 1.51 MB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 27
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# go-mongoid
**a work in progress to make go webscale**

This is a (sort of) reimplementation of [Mongoid](https://github.com/mongodb/mongoid) for Go, using [mongo-go-driver](https://github.com/mongodb/mongo-go-driver) as the connection interface. The primary focus is on ease of use, convenience.
> Mongoid is an ODM (Object-Document Mapper) framework for MongoDB in ~Ruby~ Go
Many things (most things?) don't directly translate from Ruby to Go, but major themes of the Document-related interfaces from the Mongoid API are replicated as closely as possible, with adjustments as needed to facilitate language differences.
Also, I don't represent or work for MongoDB, Inc.
# Target features for v1.0.0
- Uses Go structs as the primary document interface - ie, build your own custom document definitions using native syntax
- Supports all builtin Go data-types as document field-types
- Supports custom structs as document field-types (embedded documents)
- Supports maps and slices/arrays as dynamic/flexible field-types
- Supports custom field data-types (custom structs with their own bson marshaling methods)
- Default values for new document objects
- Change tracking - identify which fields have been altered since new object creation or since loading from the database, as well as the previous values
- Atomic updates - only changed fields are written to the datastore during save operations, same as Ruby Mongoid
- Query builder interface - concatenating method calls to build complex queries
---
# Future features
- Save and recall query Scopes (as well as default scopes per ModelType)
- Model relationships: one-to-one, one-to-many, many-to-many (and the inverses)
- Lazy loading for cross-document associations by default
- Easy basis to spawn new custom Query builders
- Custom Callbacks based on document lifecycle events (onCreate, onUpdate, onDelete)
- Custom Validations for document lifecycle events (onCreate, onUpdate, onDelete)
- Plugin architecture allows for adhoc add-on functionality (think Mongoid::Paranoia, Mongoid::Versioning, etc)
- MongoDB connection configuration via JSON, YAML, or ENV vars
# Installation & Usage
Add the library to your project
```bash
cd ~/yourGoProjectDir
go get -u github.com/eshork/go-mongoid
```
Configure a MongoDB server
```
import mongoid "https://github.com/eshork/go-mongoid"
gMongoidConfig := mongoid.Config{
Clients: []mongoid.Client{
{
Name: "default",
Hosts: []string{"localhost:27017"},
Database: "yourDbNameHere",
},
},
}
mongoid.Configure(&gMongoidConfig)
```
Define a document model and register it
```
type MyDocument struct {
mongoid.Base
MyValue string
}
var MyDocuments = mongoid.Register(&MyDocument{})
```
Make a new item and save it
```go
newDoc := MyDocuments.New().(*MyDocument)
newDoc.MyValue = "something noteworthy"
newDoc.Save()
var mongoid.ObjectID recordId = newDoc.ID
```
Retrieve stored records by ID
```go
foundDoc := MyDocuments.Find(recordId).One().(*MyDocument)
```
Check [the wiki](https://github.com/eshork/go-mongoid/wiki) for additional setup information and examples.
Refer to the [examples/](https://github.com/eshork/go-mongoid/tree/master/examples) directory for some use case examples to get you started.
Run `grift docs` to start a local godoc server to view the embedded source documentation.