Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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