我看到有一种方法可以为Find()
个函数设置SetLimit()
,但我没有看到任何设置FindOne()
的限制的选项,因为我们在FindOne()
中搜索单个结果,我们甚至不需要限制它?它自动处理限制吗?
try 使用1options.FindOne()`设置限制,但我看不到这样做的方法.
我看到有一种方法可以为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