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

try 使用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

Mongodb相关问答推荐

如何从文档列表的数组内部打印一个对象?

MongoDB:就地分组嵌套数组的元素

用Spring Boot查询MongoDB中的对象数组

MongoDB与合并对象聚合

如何在 mongodb 查找的外键中使用正则表达式

Mongodb 按数组元素聚合组

MongoDB:嵌套数组计数+原始文档

mongo shell 命令不接受使用 db 命令

删除一对一和一对多引用 - Mongoose

根据 Month 删除 mongodb 中的旧记录

如何将查询结果(单个文档)存储到变量中?

用 BsonRepresentation(BsonType.ObjectId) vs BsonId vs ObjectId 在 C# 中装饰属性之间的区别

如何使用 mongoexport 导出排序数据?

无法连接到远程 mongodb 服务器

Golang + MongoDB 嵌入类型(将一个 struct 嵌入到另一个 struct 中)

如何使用 MongoDB 以编程方式预拆分基于 GUID 的分片键

了解 Mongoose 中的关系和外键

mongoose字符串到 ObjectID

MongoDB备份计划

如何在第一个文档中恢复 MongoDB ChangeStream 而不仅仅是在我开始收听后更改