MongoDB - 游标方法

MongoDB - 游标方法 首页 / MongoDB入门教程 / MongoDB - 游标方法

MongoDB游标方法修改了指定查询的执行方式。以下是带有说明,语法和示例的游标方法的列表。

cursor.addOption(flag)

该方法添加" OP_QUERY"有线协议标志。添加它是为了更改查询的行为,例如tailaible标志。

示例

var t = db.myCappedCollection;
var cursor = t.find().
addOption(DBQuery.Option.tailable)
.addOption(DBQuery.Option.awaitData)

上面的示例添加了定制标志和AWAITDATA标志,以确保查询返回可定制的光标。将使用此方法生成光标,该方法在返回完整结果集后等待几秒钟。因此,在查询期间,它可以获得并返回附加数据。

无涯教程网

cursor.batchsize(size)

MongoDB 对象的批处理结果返回使用批处理大小方法指定的文档数。在许多情况下,如果无涯教程修改批处理大小,则不会影响用户或应用程序。

示例

db.inventory.find().batchSize(10)
MongoDB Cursor Methods

cursor.close()

该方法用于关闭游标并根据该方法的指令释放关联的服务器资源。服务器将自动关闭游标,其剩余结果为零或闲置了指定的时间。

链接:https://www.learnfk.comhttps://www.learnfk.com/mongodb/mongodb-cursor-methods.html

来源:LearnFk无涯教程网

示例

db.collection.find(<query>).close()

cursor.collation(<collation document>)

MongoDB collat​​ion()方法指定db.collection.find()返回的游标的排序规则。

close方法接受的整理文档:

{
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}

示例:

db.learnfk.find( { x: "a" } ).collation( { locale: "en_US", strength: 1 } )

输出:

MongoDB Cursor Methods

cursor.forEach(function)

JavaScript函数将使用forEach方法将光标应用于所有文档。

语法:

db.collection.find().forEach(<function>)

示例:

在find()方法返回的游标上调用的forEach()方法显示集合中所有用户的名称:

db.users.find().forEach( function(learnfk) { print( "user: " + editors.name ); } );

输出:

MongoDB Cursor Methods

cursor.hint(index)

查询期间将调用该方法,以覆盖MongoDB的默认索引选择和查询优化过程。

示例:

使用以下年龄查询将返回用户集合中使用"年龄"字段索引的所有文档。

db.users.find().hint( { age: 1 } )

cursor.limit()

此方法用于指定光标返回的最大文件数。它将在光标内使用,并与SQL数据库中的限制语句相媲美。

示例:

db.collection.find(<query>).limit(<number>)

cursor.map(function)

光标访问的文档使用map方法,并且还将最近的应用程序的返回值收集到数组中。

示例:

db.users.find().map( function(u) { return u.name; } );

cursor.max()

max方法用于限制find()。max()方法的结果。 MongoDB为特定索引指定排他上限,该上限提供了一种为复合键索引指定上限的方法。

例子

为集合创建以下索引:

db.products.createIndexes( [
   { "item" : 1, "type" : 1 },
   { "item" : 1, "type" : -1 },
   { "price" : 1 }])

如果使用{item:1,type:1}索引的顺序,则max()限制对等于item等于Mango且类型等于

db.products.find().max( { item: 'Mango', type: 'Alfonso' } ).hint( { item: 1, type: 1 } )

cursor.min()

限制find()的结果。 min()MongoDB按顺序指定特定索引的下限。此方法提供了一种定义复合键索引下限的方法。

语法:

{ field1: <min value>, field2: <min value2>, fieldN:<min valueN> }

示例:

首先,创建一个名为superstore的样本集合,其中包含以下文档:

db.products.insertMany([
{ "_id" : 1, "item" : "Java", "type" : "book", "price" : NumberDecimal("1.09") },
{ "_id" : 2, "item" : "MongoDB", "type" : "book", "price" : NumberDecimal("1.9") },
{ "_id" : 3, "item" : "Homefoil","type" : "Utensil", "price" : NumberDecimal("1.2") },
{ "_id" : 4, "item" : "Handwash", "type": "Utensil", "price" : NumberDecimal("1.29") },
{ "_id" : 5, "item" : "Rice", "type" : "Grocery", "price" : NumberDecimal("1.59") },
{ "_id" : 6, "item" : "Mango", "type" : "Fruit", "price" : NumberDecimal("1.29") },
{ "_id" : 7, "item" : "Orange", "type" : "Fruit", "price" : NumberDecimal("2.99") },
{ "_id" : 9, "item" : "Apple", "type" : "Fruit", "price" : NumberDecimal("1.99") },
{ "_id" : 8, "item" : "Potato", "type" : "vegetable", "price" : NumberDecimal("0.99") },
{ "_id" : 10, "item" : "Onion", "type" : "vegetable", "price" : NumberDecimal("1.39") }
])

现在,为集合创建索引:

MongoDB Cursor Methods

min()方法使用{item:1,type:1}索引的顺序将查询限制为文档。

db.products.find().min( { item: 'Java', type: 'book' } ).hint( { item: 1, type: 1 } )

cursor.tailable()

tailable方法将光标标记为可尾。它可以作为封顶馆藏的扫描仪。即使到达集合的最后一个节点,它也保持打开状态。该方法的应用程序将作为新数据插入集合中而继续运行。

语法:

cursor.tailable( { awaitData : <boolean> } )

如果将awaitdata标志设置为true,则当游标等待上限数据的末尾到达等待新数据到达时,MongoDB将在一段时间内阻塞查询线程。当将新数据插入受限制的集合时,发出阻塞线程的信号,将其唤醒并返回下一个批处理给客户端。

cursor.toarray()

该方法返回一个数组,其中所有文档都属于游标。它将所有文档加载到RAM中,并通过完全迭代游标来耗尽游标。

示例:

var allProductsArray = db.products.find().toArray();
if (allProductsArray.length > 0) { printjson (allProductsArray[0]); }

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

面试现场 -〔白海飞〕

移动端自动化测试实战 -〔思寒〕

Electron开发实战 -〔邓耀龙〕

.NET Core开发实战 -〔肖伟宇〕

用户体验设计实战课 -〔相辉〕

物联网开发实战 -〔郭朝斌〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

网络排查案例课 -〔杨胜辉〕

手把手教你落地DDD -〔钟敬〕

好记忆不如烂笔头。留下您的足迹吧 :)