Mongo shell 查询 - for 循环中的 .next() 返回最后一个输出
Mongo shell query - .next() in a for loop returning last output
在使用 for 循环时,我在 mongo shell.
中得到单个文档(最后一个)
var a = db.movies.find();
for(var b = 0; b <= 4; b++){
a.next();
}
但是使用这个我得到了正确的输出 - 4 个文档
for(var b = 0; b <= 4; b++){
printjson(a.next());
}
谁能解释一下?
因为您正在使用 next() 遍历游标而不是将其存储在某个变量或数组中。
第二部分有效,因为您正在将文档发送到打印功能,因此正在打印输出。
在使用 for 循环时,我在 mongo shell.
中得到单个文档(最后一个)var a = db.movies.find();
for(var b = 0; b <= 4; b++){
a.next();
}
但是使用这个我得到了正确的输出 - 4 个文档
for(var b = 0; b <= 4; b++){
printjson(a.next());
}
谁能解释一下?
因为您正在使用 next() 遍历游标而不是将其存储在某个变量或数组中。
第二部分有效,因为您正在将文档发送到打印功能,因此正在打印输出。