我看到了SetLimit() for Find() func的一种方法,但是我没有看到为FindOne()设置限制的任何选项,因为我们正在从FindOne()中搜索单个结果,我们甚至不需要限制它?自动处理极限?
尝试使用1options.FindOne()`设置限制,但我没有找到这样做的方法。
发布于 2022-10-31 07:44:31
它没有文档化,但众所周知,Collection.FindOne()意味着Limit=1的行为。Collection.FindOne()的返回值不允许访问多个结果文档,这就是为什么options.FindOne甚至没有一个SetLimit()方法的原因。
如果你检查一下源代码,它就在里面:
// 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 *int64https://stackoverflow.com/questions/74259580
复制相似问题