本文整理匯總了Golang中labix/org/v2/mgo.Collection.EnsureIndex方法的典型用法代碼示例。如果您正苦於以下問題:Golang Collection.EnsureIndex方法的具體用法?Golang Collection.EnsureIndex怎麽用?Golang Collection.EnsureIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類labix/org/v2/mgo.Collection
的用法示例。
在下文中一共展示了Collection.EnsureIndex方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: addPost
func addPost(database *mgo.Database, collection *mgo.Collection, title, subtitle, slug, category, body, image string) (post models.Post) {
// Index
index := mgo.Index{
Key: []string{"shortid", "timestamp", "title", "tags"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := collection.EnsureIndex(index)
if err != nil {
panic(err)
}
// Insert Dataz
err = collection.Insert(&models.Post{
ShortID: models.GetNextSequence(database),
Title: title,
Category: category,
Slug: slug,
Subtitle: subtitle,
Body: body,
Timestamp: time.Now(),
Published: false})
if err != nil {
panic(err)
}
result := models.Post{}
collection.Find(bson.M{"title": title}).One(&result)
return result
}
示例2: AddUser
func AddUser(collection *mgo.Collection, name, username, password string) {
// Index
index := mgo.Index{
Key: []string{"username", "email"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := collection.EnsureIndex(index)
if err != nil {
panic(err)
}
bcryptPassword, _ := bcrypt.GenerateFromPassword(
[]byte(password), bcrypt.DefaultCost)
// Insert Dataz
err = collection.Insert(&models.User{Name: name, Username: username, Password: bcryptPassword})
if err != nil {
panic(err)
}
}
示例3: EnsureIndex
// Creates an Index on the ACL collection
func (a ACL) EnsureIndex(c *mgo.Collection) error {
index := mgo.Index{
Key: []string{"Service", "Object", "Key", "User"},
Unique: true,
DropDups: false,
Background: false,
Sparse: false,
}
return c.EnsureIndex(index)
}
示例4: MakeIndex
// MakeIndex creates index on given collection
func MakeIndex(c *mgo.Collection) {
defer iendtimer(starttimer())
index := mgo.Index{
Key: []string{"name"},
Unique: false,
DropDups: false,
Background: false,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
示例5: chunkdata
func chunkdata(c *mgo.Collection) {
var err error
// Enable a compund key for the x, y and z coordinate.
index := mgo.Index{
Key: []string{"x", "y", "z"}, // Set a compound index
Unique: true,
DropDups: true,
}
err = c.EnsureIndex(index)
if err != nil {
log.Println("chunkdb EnsureIndex compound", err)
}
// Define another key for the avatar ID
err = c.EnsureIndexKey("avatarID")
if err != nil {
log.Println("chunkdb EnsureIndex avatarID", err)
}
}