Ejemplo n.º 1
0
func (theDB *DB) InsertItem(collection string, item *models.Item) {
	item.SetUniqueId()
	err := theDB.session.DB(theDB.name).C(collection).Insert(item)
	if err != nil {
		printDBError(err)
	}
}
Ejemplo n.º 2
0
func (theDB *DB) UpdateItemById(collection string, id string, partialItem *models.Item) error {
	if !bson.IsObjectIdHex(id) {
		return ErrInvalidId
	}
	oid := bson.ObjectIdHex(id)

	if partialItem.Id != "" && partialItem.Id != oid {
		return ErrUnequalUpdateIds
	}
	updateMap := map[string]interface{}{
		"$set": partialItem.CreatePartialUpdateMap(),
	}
	if err := theDB.session.DB(theDB.name).C(collection).UpdateId(oid, updateMap); err != nil {
		printDBError(err)
		return ErrCouldNotUpdateDoc
	}

	return nil
}