Created
April 16, 2019 11:22
-
-
Save JulienBreux/43ba2a821d790b5936c6d7e4122da92b to your computer and use it in GitHub Desktop.
mgo vs mongo-go-driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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