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 }
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) } }
// 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) }
// 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) } }
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) } }