Database Manual / Reference / mongosh Methods / Bulk Operations

Bulk.find.hint() (mongosh method方法)

Tip

MongoDB also provides the Mongo.bulkWrite() method for performing bulk write operations.MongoDB还提供了Mongo.bulkWrite()方法来执行批量写入操作。

Description描述

Bulk.find.hint()

Sets the hint option that specifies the index to support the Bulk.find() for:设置hint选项,指定支持Bulk.find()索引

The option can take an index specification document or the index name string.该选项可以采用索引规范文档或索引名称字符串。

If you specify an index that does not exist, the operation errors.如果指定的索引不存在,则操作会出错。

Bulk.find.hint() has no effect on Bulk.find.removeOne()Bulk.find.removeOne()没有影响

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

Example示例

Create an example collection orders:创建示例集合orders

db.orders.insertMany( [
{ "_id" : 1, "item" : "abc", "price" : Decimal128("12"), "quantity" : 2, "type": "apparel" },
{ "_id" : 2, "item" : "jkl", "price" : Decimal128("20"), "quantity" : 1, "type": "electronics" },
{ "_id" : 3, "item" : "abc", "price" : Decimal128("10"), "quantity" : 5, "type": "apparel" },
{ "_id" : 4, "item" : "abc", "price" : Decimal128("8"), "quantity" : 10, "type": "apparel" },
{ "_id" : 5, "item" : "jkl", "price" : Decimal128("15"), "quantity" : 15, "type": "electronics" }
] )

Create the following indexes on the example collection:在示例集合上创建以下索引:

db.orders.createIndex( { item: 1 } );
db.orders.createIndex( { item: 1, quantity: 1 } );
db.orders.createIndex( { item: 1, price: 1 } );

The following bulk operations specify different index to use for the various update/replace document operations:以下批量操作指定了用于各种更新/替换文档操作的不同索引:

var bulk = db.orders.initializeUnorderedBulkOp();
bulk.find({ item: "abc", price: { $gte: Decimal128("10") }, quantity: { $lte: 10 } }).hint({item: 1, quantity: 1}).replaceOne( { item: "abc123", status: "P", points: 100 } );
bulk.find({ item: "abc", price: { $gte: Decimal128("10") }, quantity: { $lte: 10 } }).hint({item: 1, price: 1}).updateOne( { $inc: { points: 10 } } );
bulk.execute();

To view the indexes used, you can use the $indexStats pipeline:要查看使用的索引,可以使用$indexStats管道:

db.orders.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } } ] )