Skip to content

Instantly share code, notes, and snippets.

@JulienBreux
Created April 16, 2019 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulienBreux/43ba2a821d790b5936c6d7e4122da92b to your computer and use it in GitHub Desktop.
Save JulienBreux/43ba2a821d790b5936c6d7e4122da92b to your computer and use it in GitHub Desktop.
mgo vs mongo-go-driver
import (
"go.mongodb.org/mongo-driver/mongo"
mongooptions "go.mongodb.org/mongo-driver/mongo/options"
)
// createSlugIndex creates the slug index in Mongo collection
func createSlugIndex(ctx context.Context, c mongo.Collection) (err error) {
ptrTrue := true
ptrSlug := "slug"
_, err = c.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bsonx.Doc{{Key: ptrSlug, Value: bsonx.Int32(1)}},
Options: &mongooptions.IndexOptions{
Unique: &ptrTrue,
Name: &ptrSlug,
Background: &ptrTrue,
Sparse: &ptrTrue,
},
},
}
return
}
import mgo "gopkg.in/mgo.v2"
// createSlugIndex creates the slug index in Mongo collection
func createSlugIndex(c mgo.Collection) error {
return c.EnsureIndex(mgo.Index{
Name: "slug",
Key: []string{"slug"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment