登录
首页 >  Golang >  Go问答

设置 go mongo-driver 中 options.FindOne() 的限制方法

来源:stackoverflow

时间:2024-02-08 10:00:24 277浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《设置 go mongo-driver 中 options.FindOne() 的限制方法》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我看到有一种方法可以为 Find() 函数设置 SetLimit() ,但我没有看到任何选项来设置 FindOne() 的限制,因为我们正在从 FindOne() 中搜索单个结果我们甚至不必限制它?它会自动处理限制吗?

尝试使用 1options.FindOne()` 设置限制,但我看不到有办法做到这一点。


正确答案


没有记录,但常识是 Collection.FindOne() 暗示 limit=1 的行为。 collection.findone() 的返回值无法访问多个结果文档,这就是为什么 options.FindOne 甚至没有 setlimit() 方法。

如果您检查 source code,它就在那里:

// unconditionally send a limit to make sure only one document is returned and the cursor is not kept open
// by the server.
findopts = append(findopts, options.find().setlimit(-1))

请注意,FindOptions.Limit 记录了:

// Limit is the maximum number of documents to return. The default value is 0, which means that all documents matching the
// filter will be returned. A negative limit specifies that the resulting documents should be returned in a single
// batch. The default value is 0.
Limit *int64

以上就是《设置 go mongo-driver 中 options.FindOne() 的限制方法》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>