Beispiel #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",
	}
}
Beispiel #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",
	}
}
Beispiel #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",
	}
}