本文整理汇总了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
}