MongoDB Cursor方法

MongoDB Cursor方法

MongoDB的游标方法修改了指定查询的执行方式。以下是游标方法的列表,包括描述、语法和示例。

cursor.addOption(flag)

该方法添加了”OP_QUERY” wire协议标志。它用于改变类似tailaible标志的查询的行为。

示例

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

上面的示例添加了”tailable”标志和”awaitData”标志,以确保查询返回可追踪的游标。使用此方法生成游标后,游标会在返回完整结果集后等待几秒钟。因此,在查询期间,它可以获取并返回附加数据。

Cursor.batchSize(size)

MongoDB 对象返回的批量结果是使用批量大小方法指定的文档数量。在许多情况下,如果我们修改批量大小,不会对用户或应用程序产生影响。

示例

db.inventory.find().batchSize(10)

MongoDB Cursor方法

cursor.close()

关闭光标并释放与该方法相关的服务器资源的方法。当服务器没有剩余的结果集或者处于空闲状态一段指定的时间后,光标将会自动关闭。

示例

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

cursor.collation(<排序规则文件>)

MongoDB collation()方法用于指定db.collection.find()返回的游标的排序规则。

接受的排序规则文件的示例:

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

示例:

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

输出:

MongoDB Cursor方法

cursor.forEach(function)

JavaScript函数将通过使用forEach方法将应用于游标检索的所有文档。

语法:

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

示例:

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

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

输出:

MongoDB Cursor方法

cursor.hint(index)

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

示例:

使用age字段上的索引将返回用户收集中的所有文档,使用以下查询。

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

cursor.limit()

此方法用于指定游标返回的最大文档数。它将在游标内使用,并类似于SQL数据库中的LIMIT语句。

示例:

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()将限制查询项为Mango且类型为1的文档的边界以下。

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

cursor.min()

使用min()约束find()的结果。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方法

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()

该方法返回一个包含游标所属的所有文档的数组。它将所有文档加载到内存中,并通过迭代游标来完成游标的耗尽。

示例:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程