在Golang中打印MongoDB中一个集合的集合列表。

huangapple go评论76阅读模式
英文:

Print collection in a list of a collection in mongodb in golang

问题

要打印MongoDB中的集合,以下是我在Python中的代码:

print(list(MongoClient(***).get_database("ChatDB").get_collection("room_members").find({'_id.username': username})))

我正在学习Go语言,我正在尝试将上述代码翻译成Go语言。

我的代码如下:

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("*****"))
if err != nil {
    panic(err)
}
likes_collection := client.Database("ChatDB").Collection("likes")
cur, err := likes_collection.Find(context.Background(), bson.D{{}})
if err != nil {
    panic(err)
}
defer cur.Close(context.Background())
fmt.Println(cur)

然而,我得到了一些十六进制值。

英文:

To print a collection from mongodb the following is my code in python:

print(list(MongoClient(***).get_database("ChatDB").get_collection("room_members".find({'_id.username': username})))

I am learning Go and I am trying to translate the aforementioned code into golang.

My code is as follows:

    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("*****"))
	if err != nil {
		panic(err)
	}
	likes_collection := client.Database("ChatDB").Collection("likes")
	cur, err := likes_collection.Find(context.Background(), bson.D{{}})
	if err != nil {
		panic(err)
	}
	defer cur.Close(context.Background())
	fmt.Println(cur)

However, I get some hex value

答案1

得分: 3

以下是翻译好的内容:

在Go语言中,Mongo的API与Mongo不同。

Find方法返回的是游标而不是集合。

你应该将你的代码改为:

var items []Items
cur, err := likes_collection.Find(context.Background(), bson.D{{}})
if err != nil {
    panic(err)
}
cur.All(context.Background(), &items)
英文:

Mongo in go lang different api than mongo.

Find returns cursor not collection.

You should changed your code to :

var items []Items 
cur, err := likes_collection.Find(context.Background(), bson.D{{}})
    if err != nil {
        panic(err)
    }
cur.All(context.Background(),&items)

答案2

得分: 0

ctx, _ := context.WithTimeout(context.Background(), 10time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("
**"))
if err != nil {
panic(err)
}
var likes []bson.M
likes_collection := client.Database("ChatDB").Collection("likes")

defer client.Disconnect(ctx)

cursor, err := likes_collection.Find(context.Background(), bson.D{{}})
if err != nil {
panic(err)
}
if err = cursor.All(ctx, &likes); err != nil {
panic(err)
}
fmt.Println(likes)

英文:
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("***"))
if err != nil {
	panic(err)
}
var likes []bson.M
likes_collection := client.Database("ChatDB").Collection("likes")

defer client.Disconnect(ctx)

cursor, err := likes_collection.Find(context.Background(), bson.D{{}})
if err != nil {
	panic(err)
}
if err = cursor.All(ctx, &likes); err != nil {
	panic(err)
}
fmt.Println(likes)

huangapple
  • 本文由 发表于 2022年4月20日 03:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/71930400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定