mongodb - Mongo Shell和代码结果返回不同的结果

标签 mongodb go

我是mongodb的新手,正在尝试按照示例进行操作,但是由于它们返回不同的结果,因此无法检查mongo shell中的数据

我的代码


    package main

    import (
        "context"
        "fmt"
        "log"

        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/mongo"
        "go.mongodb.org/mongo-driver/mongo/options"
    )

    //Trainer You will be using this Trainer type later in the program
    type Trainer struct {
        Name string
        Age  int
        City string
    }

    func main() {
        // Set client options
        clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

        // Connect to MongoDB
        client, err := mongo.Connect(context.TODO(), clientOptions)

        if err != nil {
            log.Fatal(err)
        }

        // Check the connection
        err = client.Ping(context.TODO(), nil)

        if err != nil {
            log.Fatal(err)
        }

        fmt.Println("Connected to MongoDB!")

        collection := client.Database("db_test").Collection("trainers")

        ash := Trainer{"Ash", 10, "Pallet Town"}
        misty := Trainer{"Misty", 10, "Cerulean City"}
        brock := Trainer{"Brock", 15, "Pewter City"}

        trainers := []interface{}{ash, misty, brock}

        insertResult, err := collection.InsertMany(context.TODO(), trainers)
        if err != nil {
            log.Fatal(err)
        }

        fmt.Println("Inserted a single document: ", insertResult.InsertedIDs)

        result, err := collection.Find(context.TODO(), bson.D{}, options.Find())

        if err != nil {
            log.Fatal(err)
        }
        fmt.Println("Find Failed.")

        for result.Next(context.TODO()) {
            var singleRow Trainer
            err := result.Decode(&singleRow)
            if err != nil {
                log.Fatal(err)
            }

            fmt.Println(singleRow.Name, "+", singleRow.Age)
        }

        fmt.Println("Find finished")

        err = client.Disconnect(context.TODO())

        if err != nil {
            log.Fatal(err)
        }
        fmt.Println("Connection to MongoDB closed.")

    }


这回来

    Connected to MongoDB!
    Inserted a single document:  [ObjectID("5da929e60a2ef8952d92ce8c") ObjectID("5da929e60a2ef8952d92ce8d") ObjectID("5da929e60a2ef8952d92ce8e")]
    Find Failed.
    Ash + 10
    Misty + 10
    Brock + 15
    Find finished
    Connection to MongoDB closed.


蒙古壳

是因为mongo shell和我的go代码连接到不同的集群或其他东西吗?
我也尝试使用指南针,它显示的结果也不同于mongo shell。我怎么知道外壳正常工作?
我的路径环境设置为“C:\ Program Files \ MongoDB \ Server \ 4.2 \ bin”,所以我不认为这有问题

最佳答案

替换以下行后,尝试运行您的Go代码

clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

有了这个
clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb")

关于mongodb - Mongo Shell和代码结果返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58443479/

相关文章:

php - xampp ubuntu 13.04 上的 MongoDB 配置问题

Golang 中的结构文字

go - 如何设置和解析正文请求中的时间?

loops - 使用 goroutine 进行迭代会产生意想不到的结果

json - 为自定义类型声明 UnmarshalJSON 的正确方法是什么?

go - Go 例程后关闭冗余 sql.Rows 对象的推荐方法

node.js - Mongoose 的自定义错误消息

c# - 如何使用 MongoDB C# Driver 2.0 创建流畅的聚合

mongodb - Mongoose/MongoDB - 保存记录以覆盖当前数据 :

node.js - Node.js 和 MongoDb 中同步应用程序的结构