gpt4 book ai didi

mongodb - MongoCollection.find() 会搜索两次吗?

转载 作者:可可西里 更新时间:2023-11-01 10:08:21 26 4
gpt4 key购买 nike

我正在使用如下所示的代码:

if (collection.find(toFind) != null) {
dataFound = collection.find(toFind).first();
} else {
System.err.println("NULL");
}

由于 collection.find() 在这里被调用了两次,是否会在数据库上执行 2 次搜索,或者因为它返回一个 FindIterable,它只是一个光标???

我们限制数据库操作并避免尽可能多的必要,因为我们按请求单位付费

最佳答案

为什么不存储查找结果,然后使用存储变量

var queryResult = collection.find(toFind)
if(queryResult != null){
dataFound = queryResult.first()
} else { // Handle error here}

或者更好的是只使用 findOne 方法来获取第一个结果

var queryResult = collection.findOne(toFind)
if(!queryResult){
//Handle result here
}

回答这个问题,是的,它将执行两次查询。

关于mongodb - MongoCollection.find() 会搜索两次吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56834697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com