...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/integration/insert_test.go

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

     1  // Copyright (C) MongoDB, Inc. 2022-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 integration
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    14  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    15  	"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
    16  	"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
    17  )
    18  
    19  func TestInsert(t *testing.T) {
    20  	t.Skip()
    21  	topo, err := topology.New(nil)
    22  	if err != nil {
    23  		t.Fatalf("Couldn't connect topology: %v", err)
    24  	}
    25  	_ = topo.Connect()
    26  
    27  	doc := bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))
    28  
    29  	iop := operation.NewInsert(doc).Database("foo").Collection("bar").Deployment(topo)
    30  	err = iop.Execute(context.Background())
    31  	if err != nil {
    32  		t.Fatalf("Couldn't execute insert operation: %v", err)
    33  	}
    34  	t.Log(iop.Result())
    35  
    36  	fop := operation.NewFind(bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))).
    37  		Database("foo").Collection("bar").Deployment(topo).BatchSize(1)
    38  	err = fop.Execute(context.Background())
    39  	if err != nil {
    40  		t.Fatalf("Couldn't execute find operation: %v", err)
    41  	}
    42  	cur, err := fop.Result(driver.CursorOptions{BatchSize: 2})
    43  	if err != nil {
    44  		t.Fatalf("Couldn't get cursor result from find operation: %v", err)
    45  	}
    46  	for cur.Next(context.Background()) {
    47  		batch := cur.Batch()
    48  		docs, err := batch.Documents()
    49  		if err != nil {
    50  			t.Fatalf("Couldn't iterate batch: %v", err)
    51  		}
    52  		for i, doc := range docs {
    53  			t.Log(i, doc)
    54  		}
    55  	}
    56  }
    57  

View as plain text