...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/unified/cursor_operation_execution.go

Documentation: go.mongodb.org/mongo-driver/mongo/integration/unified

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package unified
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  
    13  	"go.mongodb.org/mongo-driver/bson"
    14  )
    15  
    16  func executeIterateOnce(ctx context.Context, operation *operation) (*operationResult, error) {
    17  	cursor, err := entities(ctx).cursor(operation.Object)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	// TryNext will attempt to get the next document, potentially issuing a single 'getMore'.
    23  	if cursor.TryNext(ctx) {
    24  		// We don't expect the server to return malformed documents, so any errors from Decode here are treated
    25  		// as fatal.
    26  		var res bson.Raw
    27  		if err := cursor.Decode(&res); err != nil {
    28  			return nil, fmt.Errorf("error decoding cursor result: %w", err)
    29  		}
    30  
    31  		return newDocumentResult(res, nil), nil
    32  	}
    33  	return newErrorResult(cursor.Err()), nil
    34  }
    35  
    36  func executeIterateUntilDocumentOrError(ctx context.Context, operation *operation) (*operationResult, error) {
    37  	cursor, err := entities(ctx).cursor(operation.Object)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	// Next will loop until there is either a result or an error.
    43  	if cursor.Next(ctx) {
    44  		// We don't expect the server to return malformed documents, so any errors from Decode are treated as fatal.
    45  		var res bson.Raw
    46  		if err := cursor.Decode(&res); err != nil {
    47  			return nil, fmt.Errorf("error decoding cursor result: %w", err)
    48  		}
    49  
    50  		return newDocumentResult(res, nil), nil
    51  	}
    52  	return newErrorResult(cursor.Err()), nil
    53  }
    54  

View as plain text