Docs Menu

Bulk.find.hint()

On this page

  • Description
  • Example
Tip
Bulk.find.hint()

New in version 4.2.1.

Sets the hint option that specifies the index to support the Bulk.find() for:

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

Create an example collection orders:

db.orders.insert([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type": "apparel" },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type": "electronics" },
{ "_id" : 3, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type": "apparel" },
{ "_id" : 4, "item" : "abc", "price" : NumberDecimal("8"), "quantity" : 10, "type": "apparel" },
{ "_id" : 5, "item" : "jkl", "price" : NumberDecimal("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: NumberDecimal("10") }, quantity: { $lte: 10 } }).hint({item: 1, quantity: 1}).replaceOne( { item: "abc123", status: "P", points: 100 } );
bulk.find({ item: "abc", price: { $gte: NumberDecimal("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:

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

On this page

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.