Qmgo
is a MongoDB
dirver
for Go
. It is based on Mongo official driver, but easier to use like mgo (such as the chain call).
-
Qmgo
can allow user to use the new features ofMongoDB
in a more elegant way. -
Qmgo
is the first choice for migrating frommgo
to the newMongoDB driver
with minimal code changes.
-Go 1.10
and above.
-MongoDB 2.6
and above.
The recommended way is to use go mod
to automatically install dependencies by import github.com/qiniu/qmgo
and build
.
Of course, the following methods are also feasible:
go get github.com/qiniu/qmgo
- Start,
import
and create a new connection
import(
"context"
"github.com/qiniu/qmgo"
)
ctx := context.Background()
client, err := qmgo.NewClient(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017"})
db := client.Database("class")
coll := db.Collection("user")
If your connection points to a fixed database and collection, we recommend using the following more convenient method to initialize the connection. The subsequent operations are based on cli
and no longer need to care about the database and collection.
cli, err := qmgo.Open(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017", Database: "class", Coll: "user"})
The following examples will be based on cli
, if you use the first method for initialization, replace cli
with coll
After the initialization is successful, please defer
to close the connection
defer func() {
if err = cli.Close(ctx); err != nil {
panic(err)
}
}()
- Create index
Before doing the operation, we first initialize some data:
type BsonT map[string]interface{}
type UserInfo struct {
Name string `bson:"name"`
Age uint16 `bson:"age"`
Weight uint32 `bson:"weight"`
}
var oneUserInfo = UserInfo{
Name: "xm",
Age: 7,
Weight: 40,
}
Create index
cli.EnsureIndexes(ctx, []string{"name"}, []string{"age", "name,weight"})
- Insert a document
// insert one document
result, err := cli.Insert(ctx, oneUserInfo)
- Find a document
// find one document
one := UserInfo{}
err = cli.Find(ctx, BsonT{"name": oneUserInfo.Name}).One(&one)
- Delete documents
err = cli.Remove(ctx, BsonT{"age": 7})
- Insert multiple data
// batch insert
var batchUserInfoI = []interface{}{
UserInfo{Name: "wxy", Age: 6, Weight: 20},
UserInfo{Name: "jZ", Age: 6, Weight: 25},
UserInfo{Name: "zp", Age: 6, Weight: 30},
UserInfo{Name: "yxw", Age: 6, Weight: 35},
}
result, err = cli.Collection.InsertMany(ctx, batchUserInfoI)
- Search all, sort and limit
// find all, sort and limit
batch := []UserInfo{}
cli.Find(ctx, BsonT{"age": 6}).Sort("weight").Limit(7).All(&batch)
- Supported
- CRUD to documents
- Create indexes
- Sort、limit、count
- TODO
- Transaction
- Aggregate
- Options for every operation
Below we give an example of multi-file search、sort and limit to illustrate the similarities between qmgo
and mgo
and the improvement compare to go.mongodb.org/mongo-driver
.
How do we do ingo.mongodb.org/mongo-driver
:
// go.mongodb.org/mongo-driver
// find all, sort and limit
findOptions := options.Find()
findOptions.SetLimit(7) // set limit
var sorts bson.D
sorts = append(sorts, bson.E{Key: "weight", Value: 1})
findOptions.SetSort(sorts) // set sort
batch := []UserInfo{}
cur, err := coll.Find(ctx, BsonT{"age": 6}, findOptions)
cur.All(ctx, &batch)
How do we do in Qmgo
and mgo
:
// qmgo
// find all, sort and limit
batch := []UserInfo{}
cli.Find(ctx, BsonT{"age": 6}).Sort("weight").Limit(7).All(&batch)
// mgo
// find all, sort and limit
coll.Find(BsonT{"age": 6}).Sort("weight").Limit(7).All(&batch)
The Qmgo project welcomes all contributors. We appreciate your help!