Пример #1
0
func (c *C) Update(selector interface{}, update interface{}, options *UpdateOpts) error {
	collection := c.name

	multi := false
	if options != nil {
		multi = options.Multi
	}

	updates := make([]bson.M, 1)
	updates[0] = bson.M{
		"q":      selector,
		"u":      update,
		"upsert": false,
		"multi":  multi,
	}

	updateCommand := bson.D{{"update", collection}, {"updates", updates}}

	var result bson.M
	c.database.ExecuteCommand(updateCommand, &result)

	if convert.ToInt(result["ok"]) == 1 {
		return nil
	}

	writeConcernError := convert.ToBSONMap(result["writeConcernError"])
	if writeConcernError != nil {
		return WriteConcernError{
			Code:   convert.ToInt32(writeConcernError["code"]),
			ErrMsg: convert.ToString(writeConcernError["errmsg"]),
		}
	}

	writeErrors, err := convert.ConvertToBSONMapSlice(result["writeErrors"])
	if err == nil {
		errors := WriteErrors{}
		errors.Errors = make([]WriteError, len(writeErrors))
		for i := 0; i < len(writeErrors); i++ {
			writeError := WriteError{
				Index:  convert.ToInt32(writeErrors[i]["index"]),
				Code:   convert.ToInt32(writeErrors[i]["code"]),
				ErrMsg: convert.ToString(writeErrors[i]["errmsg"]),
			}
			errors.Errors[i] = writeError
		}
		return errors
	}

	return MongoError{
		message: "Something failed",
	}
}
Пример #2
0
func (c *C) Remove(selector interface{}, options *RemoveOpts) error {
	collection := c.name

	limit := 1
	if options != nil {
		if options.Multi {
			limit = 0
		}
	}

	deletes := make([]bson.M, 1)
	deletes[0] = bson.M{
		"q":     selector,
		"limit": limit,
	}

	deleteCommand := bson.D{{"delete", collection}, {"deletes", deletes}}

	var result bson.M
	c.database.ExecuteCommand(deleteCommand, &result)

	if convert.ToInt(result["ok"]) == 1 {
		return nil
	}

	writeConcernError := convert.ToBSONMap(result["writeConcernError"])
	if writeConcernError != nil {
		return WriteConcernError{
			Code:   convert.ToInt32(writeConcernError["code"]),
			ErrMsg: convert.ToString(writeConcernError["errmsg"]),
		}
	}

	writeErrors, err := convert.ConvertToBSONMapSlice(result["writeErrors"])
	if err == nil {
		errors := WriteErrors{}
		errors.Errors = make([]WriteError, len(writeErrors))
		for i := 0; i < len(writeErrors); i++ {
			writeError := WriteError{
				Index:  convert.ToInt32(writeErrors[i]["index"]),
				Code:   convert.ToInt32(writeErrors[i]["code"]),
				ErrMsg: convert.ToString(writeErrors[i]["errmsg"]),
			}
			errors.Errors[i] = writeError
		}
		return errors
	}

	return MongoError{
		message: "Something failed",
	}
}
Пример #3
0
func (c *C) Insert(docs ...interface{}) error {
	collection := c.name

	insertCommand := bson.D{{"insert", collection}, {"documents", docs}}

	var result bson.M
	c.database.ExecuteCommand(insertCommand, &result)

	if convert.ToInt(result["ok"]) == 1 {
		return nil
	}

	writeConcernError := convert.ToBSONMap(result["writeConcernError"])
	if writeConcernError != nil {
		return WriteConcernError{
			Code:   convert.ToInt32(writeConcernError["code"]),
			ErrMsg: convert.ToString(writeConcernError["errmsg"]),
		}
	}

	writeErrors, err := convert.ConvertToBSONMapSlice(result["writeErrors"])
	if err == nil {
		errors := WriteErrors{}
		errors.Errors = make([]WriteError, len(writeErrors))
		for i := 0; i < len(writeErrors); i++ {
			writeError := WriteError{
				Index:  convert.ToInt32(writeErrors[i]["index"]),
				Code:   convert.ToInt32(writeErrors[i]["code"]),
				ErrMsg: convert.ToString(writeErrors[i]["errmsg"]),
			}
			errors.Errors[i] = writeError
		}
		return errors
	}

	return MongoError{
		message: "Something failed",
	}
}
Пример #4
0
func (m *MongoDB) connectToSeed(seed string) error {
	connection := &Connection{
		address: seed,
	}

	connection.connect()

	db := &DB{
		name:  "admin",
		mongo: m,
	}
	var result bson.M
	db.run(connection, bson.M{"isMaster": 1}, &result)

	meRaw, ok := result["me"]
	me := seed
	if ok {
		me = convert.ToString(meRaw)
		_, ok := m.servers[me]
		if ok {
			connection.Close()
			return nil
		}
	}

	isMaster := convert.ToBool(result["ismaster"])
	if isMaster {
		m.master = connection
		m.conn = connection
	}

	m.servers[me] = connection

	hostsRaw, ok := result["hosts"]
	if ok {
		hosts, err := convert.ConvertToStringSlice(hostsRaw)
		if err != nil {
			return err
		}
		for i := 0; i < len(hosts); i++ {
			m.connectToSeed(hosts[i])
		}
	}

	return nil

}