本文整理匯總了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
}