我的mongoDB数据库中有一个非常简单的条目:
{"_id":{"$oid":"609b15511a048e03dda05861"},"password":"test_password","answer":"test_answer"}当我在Atlas UI中使用过滤器参数时,我能够拉出结果。
过滤器:
{"password": "test_password"}但是,当我调用DB时,我一直收到错误mongo: no documents in result
filter := bson.M{"password": "test_password"}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err := collection.FindOne(ctx, filter).Decode(&result)
if err != nil {
log.Printf("%v", err)
}我似乎正确地连接到了集合。有什么想法吗?
发布于 2021-05-12 21:34:27
毕竟我没有正确地连接到数据库。
我是这样连接到我的收藏的:
collection := client.Database("DB_NAME").Collection("COLLECTION_NAME")因为它没有抛出错误,所以我错误地认为这是正确的。您可以使用以下内容查看您的集合和数据库:
databases, _ := client.ListDatabaseNames(ctx, bson.M{})
log.Printf("%v", databases)
collections, _ := client.Database("DATABASES").ListCollectionNames(ctx, bson.M{})
log.Printf("%v", collections)https://stackoverflow.com/questions/67503880
复制相似问题