本文整理汇总了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)
}
}