Пример #1
0
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
0
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
0
// 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
0
// 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
0
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)
	}
}