Esempio n. 1
0
func HandleDelete(collName string, one bool, selector bson.M) (err error) {
	// Mongo session
	session, needclose, err := DAO.SessMng.GetDefault()
	if err != nil {
		return
	}

	// Close session if it's needed
	if needclose {
		defer session.Close()
	}

	// Mongo Collection
	col := getCollection(collName, session)

	if len(selector) == 0 {
		err = errors.New("can not drop entire collection, selector can not be empty")
		return
	}

	// Remove one document if no query, otherwise remove all matching query
	if one {
		err = col.Remove(selector)
	} else {
		_, err = col.RemoveAll(selector)
	}

	return
}
Esempio n. 2
0
func HandleQueryAll(documents interface{}, queryStruct QueryStruct) (total int, err error) {
	// Mongo session
	session, needclose, err := DAO.SessMng.GetDefault()
	if err != nil {
		return
	}
	// Close session if it's needed
	if needclose {
		defer session.Close()
	}

	// Compose a query from request
	query, err := composeQuery(session, queryStruct, false)
	if err != nil {
		return
	}

	// Get all documents
	if err = query.All(documents); err != nil {
		return
	}

	// Count documents if count parameter is included in query
	query.Skip(0)
	query.Limit(0)
	if n, err := query.Count(); err == nil {
		total = n
	}
	return
}
Esempio n. 3
0
func HandleQueryOne(document interface{}, queryStruct QueryStruct) (err error) {
	// Mongo session
	session, needclose, err := DAO.SessMng.GetDefault()
	if err != nil {
		return
	}
	// Close session if it's needed
	if needclose {
		defer session.Close()
	}
	// Compose a query from request
	query, err := composeQuery(session, queryStruct, true)
	if err != nil {
		return
	}
	// Get one document
	err = query.One(document)
	return
}
Esempio n. 4
0
func HandlePartialUpdateByQuery(collName string, selector bson.M, document bson.M) (err error) {
	// Mongo session
	session, needclose, err := DAO.SessMng.GetDefault()
	if err != nil {
		return
	}

	// Close session if it's needed
	if needclose {
		defer session.Close()
	}

	// Mongo Collection
	col := getCollection(collName, session)

	change := bson.M{"$set": document}
	err = col.Update(selector, change)

	return
}
Esempio n. 5
0
func HandleUpdateOne(document interface{}, queryStruct QueryStruct) (created bool, err error) {
	// Mongo session
	session, needclose, err := DAO.SessMng.GetDefault()
	if err != nil {
		return
	}

	// Close session if it's needed
	if needclose {
		defer session.Close()
	}

	// Mongo Collection
	col := getCollection(queryStruct.CollectionName, session)

	// Update document(/s)
	// replace dot in field name to /u2024
	// FIXME: replace
	// st := reflect.TypeOf(document)
	newbson := remove_dots(document)
	in, _ := bson.Marshal(newbson)
	bson.Unmarshal(in, document)

	var (
		info *mgo.ChangeInfo
	)

	// Update document by id
	info, err = col.UpsertId(queryStruct.Selector[ParamID], document)
	if err != nil {
		return
	}

	// Get updated id from mongo
	if info != nil && info.UpsertedId != nil {
		// docid, _ = info.UpsertedId.(string)
		created = (info.Updated == 0)
	}

	return
}
Esempio n. 6
0
func HandleInsert(collName string, document interface{}) (err error) {
	// Mongo session
	session, needclose, err := DAO.SessMng.GetDefault()
	if err != nil {
		return
	}
	// Close session if it's needed
	if needclose {
		defer session.Close()
	}

	// Mongo Collection
	col := getCollection(collName, session)

	// Insert document to collection
	if err = col.Insert(document); err != nil {
		logrus.Errorf("Error inserting document: %v", err)
		return
	}
	return
}