mongodb Cursor

来源:互联网 发布:iphone解锁mac 编辑:程序博客网 时间:2024/04/29 15:15

Cursor method:

let myCursor = db.collection.find();

cursor.limit(): 设置游标返回文档的个数

findOne() = find().limit(1);


cursor.next() 和 cursor.hasNext()

这两函数长配合使用:

while(myCursor.hasNext()){       document = myCursor.next();}

cursor.toArray()  将匹配到的documents保存在数组中并返回

注:调用此函数,会将所有匹配的ducument全部加载到内存,因此数据过多时,小心就使用


cursor.noCursorTimeout()游标的存在是有超时时间的“By default, the server will automatically close the cursor after 10 minutes of inactivity” ,官方原话,如果游标处于不活跃状态持续10分钟后,将会被释放。此函数,可以得到一个没有超时时间的游标。使用方法:

let myCursor = myCursor.noCursorTimeout()

当我们设定一个无超时时间的游标时,需要手动去释放它

myCursor.close()


cursor[index] 

let document = myCursor[1];


参考资料:https://docs.mongodb.com/manual/reference/method/#js-query-cursor-methods


0 0
原创粉丝点击