當前位置: 首頁>>代碼示例>>Golang>>正文


Golang DB.BulkOperationMGO方法代碼示例

本文整理匯總了Golang中github.com/coralproject/shelf/internal/platform/db.DB.BulkOperationMGO方法的典型用法代碼示例。如果您正苦於以下問題:Golang DB.BulkOperationMGO方法的具體用法?Golang DB.BulkOperationMGO怎麽用?Golang DB.BulkOperationMGO使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/coralproject/shelf/internal/platform/db.DB的用法示例。


在下文中一共展示了DB.BulkOperationMGO方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: viewSave

// viewSave retrieve items for a view and saves those items to a new collection.
func viewSave(context interface{}, mgoDB *db.DB, v *view.View, viewParams *ViewParams, ids []string, embeds embeddedRels) error {

	// Determine the buffer limit that will be used for saving this view.
	if viewParams.BufferLimit != 0 {
		bufferLimit = viewParams.BufferLimit
	}

	// Form the query.
	q := bson.M{"item_id": bson.M{"$in": ids}}
	results, err := mgoDB.BatchedQueryMGO(context, v.Collection, q)
	if err != nil {
		return err
	}

	// Set up a Bulk upsert.
	tx, err := mgoDB.BulkOperationMGO(context, viewParams.ResultsCollection)
	if err != nil {
		return err
	}

	// Group the embedded relationships by item and predicate/tag.
	embedByItem, err := groupEmbeds(embeds)
	if err != nil {
		return err
	}

	// Iterate over the view items.
	var queuedDocs int
	var result item.Item
	for results.Next(&result) {

		// Get the related IDs to embed.
		itemEmbeds, ok := embedByItem[result.ID]
		if ok {

			// Put the related IDs in the item.
			relMap := make(map[string]interface{})
			for k, v := range itemEmbeds {
				relMap[k] = v
			}
			result.Related = relMap
		}

		// Queue the upsert of the result.
		tx.Upsert(bson.M{"item_id": result.ID}, result)
		queuedDocs++

		// If the queued documents for upsert have reached the buffer limit,
		// run the bulk upsert and re-initialize the bulk operation.
		if queuedDocs >= bufferLimit {
			if _, err := tx.Run(); err != nil {
				return err
			}
			tx, err = mgoDB.BulkOperationMGO(context, viewParams.ResultsCollection)
			if err != nil {
				return err
			}
			queuedDocs = 0
		}
	}
	if err := results.Close(); err != nil {
		return err
	}

	// Run the bulk operation for any remaining queued documents.
	if _, err := tx.Run(); err != nil {
		return err
	}

	return nil
}
開發者ID:coralproject,項目名稱:xenia,代碼行數:72,代碼來源:wire.go


注:本文中的github.com/coralproject/shelf/internal/platform/db.DB.BulkOperationMGO方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。